我有一个 wcf 服务,我在控制台应用程序中自行托管。
当我运行该服务并将其部署到一台机器(称为 MyWCFRunningMachine)时,我可以转到“您已创建服务”页面。(http://MyWCFRunningMachine:8090/MyService)。
但随后它提供了一个指向 wsdl 页面的链接。该链接如下所示: http:// localhost :8090/MyService?wsdl
因此,当我单击该链接时,它会尝试使用我的机器而不是 MyWCFRunningMachine 连接到服务。
如果我冷输入 wsdl 的路径 (http://MyWCFRunningMachine:8090/MyService?wsdl),那么我会在浏览器中看到一个 wsdl。但是,如果我尝试添加服务引用,则会收到此错误:
该文件已被理解,但无法处理。
- WSDL 文档包含无法解析的链接。
- 下载“http:// localhost :8090/MyService?xsd=xsd0”时出错。
这也是在不应该引用 localhost 时。
这是我用来自托管我的服务的代码:
public class SelfServiceHost
{
static string StartUpUrl {get{return "http://localhost:8090/MyService";}}
static void Main(string[] args) { StartupService(StartUpUrl); }
public static ServiceHost StartupService(string startUpUrl)
{
//+ Setup the Service
//Create a URI to serve as the base address
Uri httpUrl = new Uri(startUpUrl);
//Create ServiceHost
ServiceHost host = new ServiceHost(typeof(MyService), httpUrl);
//Add a service endpoint
host.AddServiceEndpoint(typeof(IMyService), new WSHttpBinding(), "");
//Enable metadata exchange
ServiceMetadataBehavior serviceMetadataBehavior =
new ServiceMetadataBehavior {HttpGetEnabled = true};
host.Description.Behaviors.Add(serviceMetadataBehavior);
//! Turn on Debug. Remove for production!
host.Description.Behaviors.Remove(typeof (ServiceDebugBehavior));
ServiceDebugBehavior serviceDebugBehavior =
new ServiceDebugBehavior {IncludeExceptionDetailInFaults = true};
host.Description.Behaviors.Add(serviceDebugBehavior);
//Start the Service
host.Open();
Console.WriteLine("Service is hosted at " + httpUrl);
Console.ReadLine();
return host;
}
}
我怎样才能得到这个来删除本地主机?(注意:我不能将它硬编码到 MyWCFRunningMachine。该服务将在几台不同的机器上运行。
我是否需要使用在移动机器时更改的配置文件?(我一直远离配置文件,因为我不想为我的控制台应用程序设置一个,但如果这是唯一的方法,那么我会这样做。)