wcf - 将 WCF 服务的编程配置转换为配置文件

标签 wcf web-services configuration app-config

我编写了一个以编程方式配置的简单 WCF Web 服务。它公开了三个端点,它们将不同的绑定(bind)绑定(bind)到同一个合约:

  • WebHttpBinding
  • WebHttpRelayBinding(通过 Microsoft azure)
  • myBinding(在附加 DLL 中自制绑定(bind))

  • 配置代码目前非常简单:
    WebServiceHost host = new WebServiceHost(
        typeof(MyService), new Uri("http://localhost:80/"));
    
    host.AddServiceEndpoint(typeof(MyService), new WebHttpBinding(), "");
    
    ServiceEndpoint sbEndpoint = host.AddServiceEndpoint(
        typeof(MyService), new WebHttpRelayBinding(), "http://azureURL");
    TransportClientEndpointBehavior sbBehavior =
        new TransportClientEndpointBehavior();
    sbBehavior.CredentialType = TransportClientCredentialType.UserNamePassword;
    sbBehavior.Credentials.UserName.UserName = "azureUserName";
    sbBehavior.Credentials.UserName.Password = "azurePassword";
    sbEndpoint.Behaviors.Add(sbBehavior);
    
    host.AddServiceEndpoint(typeof(MyService), new MyBinding(), "http://someURL");
    
    host.Open();
    

    现在我想将此配置导出到配置文件中,因为我希望能够更改它而无需重新编译。

    我现在的问题是:
  • 我在哪里可以找到有值(value)的信息来实现我的目标?大多数站点只谈论 SOAP 绑定(bind) - 没有提及如何包含非标准绑定(bind)。
  • 我是否必须更改 MyBinding 才能通过 app.config 接受配置,或者 ServiceModel 在配置正常时是否像我的编程方法一样调用它?
  • 最佳答案

    好的,所以最重要的是:

  • 地址
  • 绑定(bind)
  • 契约(Contract)

  • 然后加入一些额外的东西。

    1) 地址:

    从这里得到这个:
    WebServiceHost host = new WebServiceHost(
        typeof(MyService), new Uri("http://localhost:80/"));
    host.AddServiceEndpoint(typeof(MyService), new WebHttpBinding(), "");
    

    和这里:
    ServiceEndpoint sbEndpoint = host.AddServiceEndpoint(
        typeof(MyService), new WebHttpRelayBinding(), "http://azureURL");
    

    所以你需要类似的东西:
    <endpoint address=""
    <endpoint address="http://azureURL"
    <endpoint address=""http://someURL"
    

    在您的服务中。

    2)绑定(bind):

    第一个端点是 webHttpBinding,第二个端点使用自定义绑定(bind)(“MyBinding”) - 所以你有:
    <endpoint address="" 
              binding="webHttpBinding"
    <endpoint address="http://azureURL"
              binding="webRelayHttpBinding"
    <endpoint address=""http://someURL"
              binding="myBinding"
    

    你需要定义你的自定义绑定(bind):
    <bindings>
      <customBinding>
        <binding name="MyBinding">
          .. define the parameters of your binding here
        </binding>
      </customBinding>
    </bindings>
    

    或创建 <extensions>您的绑定(bind)部分存储在单独程序集中的代码中。

    3) 契约(Contract)

    我在任何地方都没有清楚地看到契约(Contract)——你只使用过 typeof(MyService),但通常,这是具体的服务 实例 ,而不是服务 契约(Contract) 这应该是一个接口(interface)(类似于 IMyService )。为什么没有明确的服务契约(Contract)?

    无论如何,如果您的服务实现同时也是契约(Contract)(不是最佳实践!但可能),那么您的两个端点是这样的:
    <endpoint address="" 
              binding="webHttpBinding"
              contract="MyService" />
    <endpoint address="http://azureURL"
              binding="webHttpRelayBinding"
              contract="MyService" />
    <endpoint address="http://someURL"
              binding="myBinding"
              contract="MyService" />
    

    然后,您需要在这里和那里添加一些点缀(定义服务的“基地址”,为服务命名等),最终应该是这样的:
    <system.serviceModel>
        <bindings>
          <customBinding>
            <binding name="MyBinding">
              .. define the parameters of your binding here
            </binding>
          </customBinding>
        </bindings>
        <services>
          <service name="YourNameSpace.MyService">
            <host>
              <baseAddresses>
                 <add baseAddress="http://localhost:80/" />
              </baseAddresses>
             </host>
             <endpoint address="" 
                       binding="webHttpBinding"
                       contract="MyService" />
             <endpoint address="http://azureURL"
                       binding="webHttpRelayBinding"
                       contract="MyService" />
             <endpoint address="http://someURL"
                       binding="myBinding"
                       contract="MyService" />
          </service>
        </services>
    </system.serviceModel>
    

    现在你所缺少的只是定义的行为 - 我将把它作为海报的练习:-)

    这有什么帮助吗?

    至于引用 - 嗯......很难说......我想通常的书籍(MLBustamante 的初学者/中级的“Learning WCF”,Juval Lowy 的中级/高级的“Programming WCF”)是我最好的打赌,还有很多经验,真的。我不知道任何明确显示和教授如何在代码和配置中的设置之间进行转换的来源 - 提到的两本书通常显示两种方式,从这里,你可以自己弄清楚。

    马克

    关于wcf - 将 WCF 服务的编程配置转换为配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1278332/

    相关文章:

    c# - jQuery 使用 JSON WCF

    c# - 什么是只读集合?

    java - Apache Wink Json REST Web 服务

    .net - 是否可以出于测试目的从 Web 浏览器调用 WCF Web 服务方法?

    php - 类的大型配置,实现建议

    wcf - 客户端的WCF Web服务自定义异常错误

    .net - 在 IIS 7.0 上访问 .asmx 页面时出现 "Could not create type XXXX"

    java - android旋转屏幕导致文字颜色更改为默认

    c# - .NET 自定义配置部分 : Configuration. GetSection 引发 'unable to locate assembly' 异常

    c# - WCF 发现找到端点但主机是 "localhost"