2011-03-04 67 views
3

我需要从DLL中使用WCF服务,因此我没有任何配置文件来从中读取绑定配置。消耗WCF没有app.config

我真的很难得到它的工作。最后,作为一个非常简单的解决办法,我一个引用添加到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(); 

在本地主机这个简单的工作。当从另一台机器上尝试它,我得到这个错误:

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> 
+0

显示您的服务配置,以便我们可以看到需要什么客户端设置。很明显,你正在使用一些安全服务(WsHttpBinding默认在消息级别保护)。 – 2011-03-04 09:34:10

回答

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身份验证。

2

使用基本的HTTP绑定相反,如果你不需要安全。

+0

我不能使用基本绑定,因为它使用SOAP 1.1,WCF使用SOAP 1.2,所以它会给我协议错误。我已经试过这种方式了。 – pistacchio 2011-03-04 09:39:26

+0

basichttpbinding是标准的wcf绑定,请参阅http://msdn.microsoft.com/en-us/library/system.servicemodel.basichttpbinding.aspx,但您必须配置服务器和客户端以使用相同的绑定 – 2011-03-04 10:37:54