【问题标题】:Translate this app.config xml to code? (WCF)将此 app.config xml 转换为代码? (WCF)
【发布时间】:2009-04-09 00:42:40
【问题描述】:

我需要将以下 app.config 部分转换为代码。我已经看过几个例子,但仍然不能让它发挥作用。有人可以帮忙吗?

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="MyService" 
                 closeTimeout="00:01:00"
                 openTimeout="00:01:00" 
                 receiveTimeout="00:10:00" 
                 sendTimeout="00:01:00"
                 allowCookies="false" 
                 bypassProxyOnLocal="false" 
                 hostNameComparisonMode="StrongWildcard"
                 maxBufferSize="65536" 
                 maxBufferPoolSize="524288" 
                 maxReceivedMessageSize="65536"
                 messageEncoding="Text" 
                 textEncoding="utf-8" 
                 transferMode="Buffered"
                 useDefaultWebProxy="true">
            <readerQuotas maxDepth="32" 
                          maxStringContentLength="8192" 
                          maxArrayLength="16384"
                          maxBytesPerRead="4096" 
                          maxNameTableCharCount="16384" />
            <security mode="TransportWithMessageCredential">
                <transport clientCredentialType="None" 
                           proxyCredentialType="None" 
                           realm="" />
                <message clientCredentialType="UserName" 
                         algorithmSuite="Default" />
            </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="https://server.com/service/MyService.asmx"
      binding="basicHttpBinding" bindingConfiguration="MyService"
      contract="MyService.MyServiceInterface"
      name="MyService" />
    </client>
</system.serviceModel>

我的用例是我正在编写一个将被其他非.net 应用程序使用的 dll,因此我没有好地方放置 app.config。

谢谢!

【问题讨论】:

  • 或许能展示你目前所拥有的,人们可以尝试指出问题
  • @Sunny:感谢您将 XML 包含在内,实现该功能的诀窍是什么?

标签: .net wcf app-config


【解决方案1】:

你可以使用这样的东西(它看起来很标准 basicHttpBinding):

BasicHttpBinding binding = new BasicHttpBinding();
Uri endpointAddress = new Uri("https://server.com/service/MyService.asmx");

ChannelFactory<MyService.MyServiceInterface> factory = new ChannelFactory<MyService.MyServiceInterface>(binding, endpointAddress);

MyService.MyServiceInterface proxy = factory.CreateChannel();

只要您有一个包含可用合同(“MyService.MyServiceInterface”)的 DLL 并且您可以在您的客户端中引用它,它就可以工作。

如果您在服务端需要这个,您将不得不使用一些不同的类等 - 但基本原理是相同的(创建绑定,创建一个或多个端点地址,绑定它们)。

马克

PS:抱歉,我刚刚注意到您使用 https:// 地址 - 这可能需要在代码中进行一些额外的安全配置。

【讨论】:

    【解决方案2】:

    谢谢你,marc_s,你把我引向了正确的方向!

    对于任何感兴趣的人,这里是使其也适用于 SSL 的代码:

            BasicHttpBinding binding = new BasicHttpBinding();
            binding.Security.Mode = BasicHttpSecurityMode.TransportWithMessageCredential;
            Uri endpointAddress = new Uri("https://server.com/Service.asmx");
    
            ChannelFactory<MyService.MyServiceInterface> factory = new ChannelFactory<MyService.MyServiceInterface>(binding, endpointAddress.ToString());
            factory.Credentials.UserName.UserName = "username";
            factory.Credentials.UserName.Password = "password";
    
            MyService.MyServiceInterface client = factory.CreateChannel();
    
            // make use of client to call web service here...
    

    【讨论】:

    • 好的,很高兴我能帮上忙! (我现在正在为 WCF 考试学习,所以我应该对这些东西感到新鲜 :-)
    猜你喜欢
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-03
    • 2016-02-01
    • 1970-01-01
    • 2019-11-19
    相关资源
    最近更新 更多