【问题标题】:WCF JSON Format - while consuming in Client side getting an error messageWCF JSON 格式 - 在客户端消费时收到错误消息
【发布时间】:2018-02-23 01:31:28
【问题描述】:

客户端代码

  1. 我已经创建了 Web 应用程序并添加了对客户端项目的服务引用
  2. 我尝试像这样创建一个客户端对象:

     Service1Client a = new Service1Client();
    

但收到错误消息:

  • 在 ServiceModel 客户端中找不到引用合同“ServiceReference_test.IService1”的默认端点元素 配置部分。这可能是因为没有配置文件 为您的应用程序找到,或者因为没有匹配的端点元素 可以在客户端元素中找到此合同。

你能告诉我我做错了什么吗,我是 WCF 的新手,请帮助我

返回 JSON 格式的 WCF 服务:

namespace WcfService1
{

    public class Service1 : IService1
    {

        public string GetData(string Id)
        {
            return ("You entered: " + Id);
        }


    }
}


namespace WcfService1
{
   [ServiceContract]
    public interface IService1
    {
    [OperationContract]
    [WebInvoke(Method = "GET", UriTemplate = "/GetData/{Id}",
     RequestFormat = WebMessageFormat.Json,
     ResponseFormat = WebMessageFormat.Json,
     BodyStyle = WebMessageBodyStyle.Wrapped
     )]
        string GetData(string Id);


        // TODO: Add your service operations here
    }

}

Web.Config 文件

  <system.serviceModel>

    <services>
      <service name="WcfService1.Service1" behaviorConfiguration="ServiceBehaviour">
        <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" contract="WcfService1.IService1"/>
        <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/>
      </service>

    </services> 

    <behaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp helpEnabled="true" automaticFormatSelectionEnabled="false"  />
        </behavior>

      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <!--<protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>-->    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

【问题讨论】:

    标签: wcf


    【解决方案1】:

    您应该在您的客户端 App.configWeb.config 中添加以下标签:

    <system.serviceModel>
        <bindings>
          <webHttpBinding>
            <binding name="WebHttpBinding" />
          </webHttpBinding>
        </bindings>
        <client>
          <endpoint address="http://localhost:8733/Design_Time_Addresses/fine_service/FineService/soap"
              binding="webHttpBinding" bindingConfiguration="WebHttpBinding"
              contract="ServiceReference1.IService1" name="WebHttpBinding" />
        </client>
      </system.serviceModel>
    

    如果您的 cofig 文件中有此标签,请确保客户端端点合同名称应与您的服务端点中的相同。在您的情况下,合同名称是 IService1

    编辑: 另见this

    【讨论】:

      猜你喜欢
      • 2022-06-13
      • 1970-01-01
      • 2013-05-21
      • 2018-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      相关资源
      最近更新 更多