首页 > 解决方案 > WCF 在客户端调用服务

问题描述

我有一个带有 rest-api 端点的小 wcf 测试应用程序。当我向他们添加基本身份验证时,我收到错误消息,我必须使用 https 而不是 http。我可以在 localhost 服务器-客户端通信上使用没有 https 的基本身份验证吗?

合同:

[WebInvoke(Method = "POST",
        UriTemplate = "Auth/Create",
        RequestFormat = WebMessageFormat.Xml,
        ResponseFormat = WebMessageFormat.Xml,
        BodyStyle = WebMessageBodyStyle.Wrapped)]
    Response Create(Stream stream);

应用程序配置

<behavior name="AuthBehavior">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
<services>
  <service name="Web.Service.Core.Services.AuthContract"
           behaviorConfiguration="AuthBehavior" >
    <endpoint address="Create"
              binding="webHttpBinding" bindingConfiguration="WebHttpBindingConfig"
              contract="Web.Service.Library.Contracts.IAuthContract" />

    <endpoint address="mex"
              binding="mexHttpsBinding"
              contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost/Auth/" />
      </baseAddresses>
    </host>
  </service>

标签: c#.nethttpwcfhttps

解决方案


小伙伴,如果最后的回复对你有帮助,请标记为有用。
无法处理该消息,因为内容类型“应用程序/xml”不是预期的类型“应用程序/soap + xml”;charset = utf-8 '
错误信息可能是MEX端点配置引起的,Restful服务不需要元数据端点,我们可以直接去掉。

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

通常情况下,WebHttpBinding 创建的带有 Basic 身份验证的 WCF 可以通过 HTTP 协议实现,例如下面的示例。我使用控制台应用程序来托管服务。
服务器端。

class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost sh=new ServiceHost(typeof(MyService)))
            {
                sh.Open();
                Console.WriteLine("Service is ready....");

                Console.ReadLine();
                sh.Close();
            }
        }

    }

    [ServiceContract]
    interface IService
    {
        [OperationContract]
        [WebGet]
        string GetData();
    }
    public class MyService : IService
    {
        public string GetData()
        {
            return DateTime.Now.ToString();
        }
}

服务器端配置。

<system.serviceModel>
    <services>
      <service name="Server1.MyService">
        <endpoint address="" binding="webHttpBinding" contract="Server1.IService" bindingConfiguration="mybinding"></endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:5577"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="mybinding">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Basic"></transport>
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>

示例调用。 如果有什么我可以帮助的,请随时与我联系。
在此处输入图像描述


推荐阅读