我使用 VisualStudio 2015 创建了一个新的 WCFServiceLibrary。我执行了它,一切正常,我可以看到带有示例方法的 WCF 测试客户端,并且可以从浏览器访问。
现在我想添加一个自定义服务宿主工厂。我添加了这个类:
namespace WcfServiceLibrary1
{
public class DerivedFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
ServiceHost host = new ServiceHost(serviceType, baseAddresses);
// do something here
return host;
}
}
}
然后我将 serviceHostingEnvironment 部分添加到 app.config 中:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<serviceHostingEnvironment>
<serviceActivations>
<add service="WcfServiceLibrary1.Service1"
relativeAddress="./MyService.svc"
factory="WcfServiceLibrary1.DerivedFactory"/>
</serviceActivations>
</serviceHostingEnvironment>
<services>
<service name="WcfServiceLibrary1.Service1">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/" />
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="WcfServiceLibrary1.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
我在 DerivedFactory 类的 CreateServiceHost 方法中放置了一个断点。我执行该项目,但是:
- 我没有进入断点
- 我无法从浏览器访问:http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/MyService.svc
我已经用 relativeAddress="/MyService.svc"和 relativeAddress="MyService.svc"进行了测试,但这不起作用。
有没有办法在 Debug模式下测试自定义工厂?
服务类:
namespace WcfServiceLibrary1
{
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
}
最佳答案
这个具体的答案很简单,涉及冗余配置。为了让我们了解更多,我详细阐述了我的答案。
通过 IIS 托管服务 访问您的服务的正确方法是以下 URL http://localhost:8733/MyService.svc
这是由
<add service="WcfServiceLibrary1.Service1"
relativeAddress="./MyService.svc"
factory="WcfServiceLibrary1.DerivedFactory"/>
仅在自托管服务中您将需要baseAddress 配置,因此它是多余的,可以删除。
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/" />
</baseAddresses>
</host>
如果您想访问完整地址 http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/您需要将 relativeAddress 更改为以下内容:
relativeAddress="./Design_Time_Addresses/WcfServiceLibrary1/Service1/MyService.svc"
关于c# - 在 WCF 中调试自定义服务主机工厂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40518194/