尝试在运行wcf服务的地方之外访问时无法访问wcf服务

时间:2013-05-02 02:58:31

标签: c# wcf

我的简单wcf运行正常,因为当我尝试为wcf创建一个应用程序时,它会返回预期的数据,但是当我尝试在wcf服务运行之外运行应用程序时,它会给出错误

enter image description here

如何解决此问题?

wcf service web config

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

          <system.web>
            <compilation debug="true" targetFramework="4.0" />
          </system.web>
          <system.serviceModel>
<services>
      <service name="WcfService1.Service1">
        <endpoint address="http://192.168.21.102:4424/Service1.svc"
                  binding="wsHttpBinding"
                  contract="WcfService1.IService1"></endpoint>
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange"></endpoint>
      </service>
    </services>
            <behaviors>
              <serviceBehaviors>
                <behavior>
                  <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
                  <serviceMetadata httpGetEnabled="true"/>
                  <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
                  <serviceDebug includeExceptionDetailInFaults="false"/>
                </behavior>
              </serviceBehaviors>
            </behaviors> 
            <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
          </system.serviceModel>
         <system.webServer>
            <modules runAllManagedModulesForAllRequests="true"/>
          </system.webServer>

        </configuration>

客户端应用配置

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IService1" 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="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:4424/Service1.svc" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
                name="BasicHttpBinding_IService1" />
        </client>
    </system.serviceModel>
</configuration>

我没有触摸或编辑那些东西

1 个答案:

答案 0 :(得分:0)

两件事:

首先,您的服务定义为使用wsHttpBinding,但您的客户正在使用basicHttpBinding。绑定需要匹配。

其次,客户端配置中的地址设置为localhost - 这意味着您的客户端正在客户端所在的同一台计算机上查找服务。

例如,如果您的服务位于名为MySever1的计算机上(例如),并且您将客户端(带有发布的配置)放在名为MyClient1的计算机上(例如,再次),它将查找该服务在MyClient1上(客户端的localhost)。

将客户端端点更改为http://192.168.21.102:4424/Service1.svc,您应该可以连接,除非出现任何防火墙问题。

例如:

<client>
  <endpoint address="http://192.168.21.102:4424/Service1.svc" 
            binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_IService1" 
            contract="ServiceReference1.IService1"
            name="BasicHttpBinding_IService1" />
</client>

修改

在服务端配置中,为端点执行以下操作:

service name="WcfService1.Service1">
    <endpoint address=""
              binding="basicHttpBinding"
              contract="WcfService1.IService1">
    </endpoint>
    <endpoint address="mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange">
    </endpoint>
  </service>

在您的客户端配置中:

<client>
  <endpoint address="http://192.168.21.102:4424/Service1.svc"
            binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_IService1" 
            contract="ServiceReference1.IService1"
            name="BasicHttpBinding_IService1" />
</client>

请注意,在服务端点声明中,address属性为空 - * .svc文件的位置用于确定实际地址。其次,将绑定更改为basicHttpBinding以匹配客户端将要调用的内容。

在客户端配置中,指定要呼叫的服务的完整地址。