在没有app.config的情况下使用WCF

时间:2011-03-04 09:30:57

标签: wcf

我需要从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>

3 个答案:

答案 0 :(得分:4)

使用此代码:

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域)中使用,因为它通过defult使用消息安全性和Windows身份验证。

答案 1 :(得分:2)

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

答案 2 :(得分:0)

如果您有一个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>