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

标签 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="/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/

相关文章:

c# - 跨 AppDomains 的多个服务实例

c# - 如果 (Input.GetKeyDown(KeyCode.UpArrow)); - 统一控制台垃圾邮件

c# - 将 IEnumerable<string> 转换为字典

c# - 停止在给定时间跨度后超时的 CancellationTokenSource 以保持其状态?

wcf - 服务器使用用户帐户的 WCF Kerberos 客户端模式

wcf - C# WCF REST - 如何使用 JSON.Net 序列化器而不是默认的 DataContractSerializer?

wcf - 在客户端,WCF操作合约的返回值为null!有什么解决办法吗?

c# - EF Core 使用 NUGET 包管理器控制台或 CMD

c# - 编码我的字符串以通过 C# 发送 http 请求

wcf - WCF 场景中的自签名证书性能