加入收藏 | 设为首页 | 会员中心 | 我要投稿 济源站长网 (https://www.0391zz.cn/)- 数据工具、数据仓库、行业智能、CDN、运营!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

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

发布时间:2021-01-09 19:27:44 所属栏目:资源 来源:网络整理
导读:我编写了一个简单的WCF Web服务,该服务是通过programmaticaly配置的.它公开了三个端点,它们将不同的绑定绑定到同一个合约: WebHttpBinding WebHttpRelayBinding(在Microsoft azure上) myBinding(在附加DLL中自制绑定) 配置代码目前非常简单: WebServiceHos

我编写了一个简单的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”)是我最好的打赌,还有很多经验,真的.我不知道任何明确显示和教导如何在代码和配置之间转换设置的来源 – 提到的两本书通常都显示两种方式,从中你可以自己弄清楚.

(编辑:济源站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读