WCF 在客户端调用服务

WCF Call the service on the client-side

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

合同:

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

app.config

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

朋友,如果最后的回复对你有帮助,请标记为有用。

错误消息可能是由 MEX 端点配置引起的,Restful 服务不需要有元数据端点,我们可以直接将其删除。

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

通常情况下,通过WebHttpBinding创建的WCF和Basic认证可以通过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>

调用示例。

如果有什么可以帮到您的,请随时与我联系。