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

我编写了一个简单的WCF Web服务,该服务是通过programmaticaly配置的.它公开了三个端点,它们将不同的绑定绑定到同一个合约:

> WebHttpBinding
> WebHttpRelayBinding(在Microsoft azure上)
> myBinding(在附加DLL中自制绑定)

配置代码目前非常简单:

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();

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

我现在的问题是:

>我在哪里可以找到有价值的信息来实现我的目标?大多数网站只谈论SOAP绑定 – 没有关于如何包含非标准绑定的说法.
>我是否必须更改MyBinding才能通过app.config接受配置,或者ServiceModel是否正好调用它,就像我的编程方法在配置正常时一样?

解决方法

好的,所以最重要的是:

>地址
>绑定
>合同

然后抛出一些额外的东西.

1)地址:

从这里得到这个:

WebServiceHost host = new WebServiceHost(
    typeof(MyService),new Uri("http://localhost:80/"));
host.AddServiceEndpoint(typeof(MyService),"");

和这里:

ServiceEndpoint sbEndpoint = host.AddServiceEndpoint(
    typeof(MyService),"http://azureURL");

所以你需要这样的东西:

<endpoint address=""
<endpoint address="http://azureURL"
<endpoint address=""http://someURL"

在你的服务中.

2)绑定:

一个端点是webHttpBinding,第二个端点使用自定义绑定(“MyBinding”) – 所以你有:

<endpoint address="" 
          binding="webHttpBinding"
<endpoint address="http://azureURL"
          binding="webRelayHttpBinding"
<endpoint address=""http://someURL"
          binding="myBinding"

你需要定义你的自定义绑定:

<bindings>
  <customBinding>
    <binding name="MyBinding">
      .. define the parameters of your binding here
    </binding>
  </customBinding>
</bindings>

或创建< extensions>存储在单独程序集中的代码中的绑定部分.

3)合同

我没有在任何地方清楚地看到合同 – 你只使用typeof(MyService),但通常,这是具体的服务实例,而不是应该是接口的服务合同(类似于IMyService).你为什么没有明确的服务合同?

无论如何,如果您的服务实现也是合同,同时(不是最佳实践!但可能),那么您有两个端点,如下所示:

<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为初学者/中级学习的“学习WCF”,由Juval Lowy为中级/高级的“编程WCF”)是我最好的打赌,还有很多经验,真的.我不知道任何明确显示和教导如何在代码和配置之间转换设置的来源 – 提到的两本书通常都显示两种方式,从中你可以自己弄清楚.

相关文章

vue阻止冒泡事件 阻止点击事件的执行 &lt;div @click=&a...
尝试过使用网友说的API接口获取 找到的都是失效了 暂时就使用...
后台我拿的数据是这样的格式: [ {id:1 , parentId: 0, name:...
JAVA下载文件防重复点击,防止多次下载请求,Cookie方式快速简...
Mip是什么意思以及作用有哪些