使用WCF服务时出错

时间:2011-05-18 06:07:42

标签: .net wcf json .net-3.5

当我尝试使用我的服务时,我面临以下错误。

There was no endpoint listening at http://myip:84/service1.svc/ that could accept the message. This is often caused by an incorrect address or SOAP action.

我的服务代码:

<system.serviceModel>

        <services>
            <service name="wcftest1.Service1" behaviorConfiguration="wcftest1.Service1Behavior">


                <endpoint address="http://myip:84/Service1.svc/" behaviorConfiguration="epbeh" bindingConfiguration ="webConfig" binding="webHttpBinding" contract="wcftest1.IService1">
                </endpoint>

                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

            </service>

        </services>

        <bindings>
            <basicHttpBinding>
                <binding name="basicConfig" >
                </binding>
            </basicHttpBinding>
            <webHttpBinding>
                <binding name="webConfig">

                </binding>

            </webHttpBinding>

        </bindings>
        <behaviors>
            <serviceBehaviors>
                <behavior name="wcftest1.Service1Behavior" >
                    <!-- 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"/>
                    <serviceCredentials>
                        <userNameAuthentication userNamePasswordValidationMode="Custom"  customUserNamePasswordValidatorType="wcftest1.validateUser, wcftest1"/>
                    </serviceCredentials>

                </behavior>
            </serviceBehaviors>
            <endpointBehaviors>
                <behavior name="epbeh">
                    <webHttp />
                </behavior>
            </endpointBehaviors>
        </behaviors>

服务类:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1 : IService1
    {

服务接口:

[OperationContract]
        [WebGet(ResponseFormat= WebMessageFormat.Json)]
        string GetMsg();

客户端webconfig:

    <system.serviceModel>
        <bindings>
   <customBinding>
    <binding name="WebHttpBinding_IService1">
     <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
      messageVersion="Soap12" writeEncoding="utf-8">
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
       maxBytesPerRead="4096" maxNameTableCharCount="16384" />
     </textMessageEncoding>
        <httpTransport/>
    </binding>

   </customBinding>
  </bindings>
        <client>
   <endpoint binding="customBinding" address="http://myip:84/service1.svc/" bindingConfiguration="WebHttpBinding_IService1"
    contract="ServiceReference1.IService1" name="WebHttpBinding_IService1" />
  </client>
    </system.serviceModel>

客户端代码:

 ServiceReference1.Service1Client sc = new userwcftest1.ServiceReference1.Service1Client();

            Response.Write(sc.GetData(5));

我的服务将由PHP开发人员使用,它建立在.NET framework 3.5上,我将返回JSON。 我是WCF的新手。

请帮帮我 感谢

1 个答案:

答案 0 :(得分:0)

客户端使用与服务不同的绑定 - 客户端使用带有消息版本SOAP 1.2的自定义绑定;服务器使用webHttpBinding,它大致相当于使用MessageVersion.None的自定义绑定(它是REST,而不是SOAP)。

您在服务上添加了一个mex端点,但REST端点不会发出元数据,因此使用svcutil / add service reference生成的任何内容都不起作用。

拨打您的服务的一种方法是简单地使用以下内容:

WebClient c = new WebClient();
string result = c.DownloadString("http://myip:84/service1.svc/GetMsg");