【问题标题】:Consume WCF without app.config在没有 app.config 的情况下使用 WCF
【发布时间】:2011-07-08 16:41:06
【问题描述】:

我需要使用 DLL 中的 WCF 服务,因此我没有任何配置文件可以从中读取绑定配置。

我很难让它发挥作用。最后,作为一个非常简单的解决方案,我添加了对 WCF 的引用并像这样实例化它:

        WSHttpBinding binding = new WSHttpBinding();
        EndpointAddress address = new EndpointAddress("http://myhost.net/Service.svc");

        ServiceReference.ServiceClient client = new ServiceReference.ServiceClient(binding, address);
        var result = client.Method1();

在 localhost 中,这很简单。在另一台机器上尝试时,我收到此错误:

The request for security token could not be satisfied because authentication failed.

在主机中,IIS 设置为“匿名”,所以我想它应该可以正常工作。

有什么帮助吗?

编辑:服务配置文件

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="Mai.MyPlanner.Service">
        <endpoint address="" binding="wsHttpBinding" contract="Mai.MyPlanner.IService">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://MY_SERVICE"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>

          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

<connectionStrings>
  <!-- PROD -->

  <!-- TEST -->
</connectionStrings>

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

【问题讨论】:

  • 显示您的服务配置,以便我们查看需要哪些客户端设置。显然,您在服务中使用了一些安全性(默认情况下,WsHttpBinding 在消息级别上是安全的)。

标签: wcf


【解决方案1】:

使用此代码:

WSHttpBinding binding = new WSHttpBinding();
EndpointIdentity identity = EndpointIdentity.CreateDnsIdentity("localhost");
EndpointAddress address = new EndpointAddress("http://myhost.net/Service.svc", identity);

ServiceReference.ServiceClient client = new ServiceReference.ServiceClient(binding, address);
var result = client.Method1();

您仍然需要将 Dns 标识值和地址传递到调用此代码的方法中。此外,这种类型的配置只能在 Intranet(相同的 Windows 域)中使用,因为它默认使用消息安全和 Windows 身份验证。

【讨论】:

    【解决方案2】:

    如果您不需要安全性,请改用基本的 http 绑定。

    【讨论】:

    • 我不能使用基本绑定,因为它使用 SOAP 1.1 而 WCF 使用 SOAP 1.2,所以它会给我协议错误。我已经试过了。
    • basichttpbinding 是一个标准的 wcf 绑定,请参阅msdn.microsoft.com/en-us/library/…,但您必须将服务器和客户端都配置为使用相同的绑定
    【解决方案3】:

    如果您有一个 ServiceHost 会采用如下所示的 ServiceEndpoint:

    // 步骤 3 添加服务端点。 selfHost.AddServiceEndpoint(typeof(ICalculator), binding, "CalculatorService");

    您仍将如何以编程方式指定 App.config 元素,例如:

    <services>
      <service name="GettingStartedLib.CalculatorService">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8000/GettingStarted/CalculatorService" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address="" binding="wsHttpBinding" contract="GettingStartedLib.ICalculator">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <!-- Metadata Endpoints -->
        <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. --> 
        <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-20
      • 2012-05-07
      • 2015-01-23
      • 1970-01-01
      相关资源
      最近更新 更多