在WCF中调试自定义服务主机工厂

时间:2016-11-10 00:27:38

标签: c# wcf

我使用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中放了一个断点。我执行项目,但是:

我已经使用relativeAddress =&#34; /MyService.svc"进行了测试;和relativeAddress =&#34; MyService.svc&#34;,但这不起作用。

有一种方法可以在调试模式下测试自定义工厂吗?

服务类:

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;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

这个具体的答案很简单,涉及冗余配置。为了让我们更了解,我已经详细说明了我的答案。

通过IIS托管服务以正确的方式访问您的服务的是以下网址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"