c# - 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>
解决方案
小伙伴,如果最后的回复对你有帮助,请标记为有用。
无法处理该消息,因为内容类型“应用程序/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>
推荐阅读
- javascript - 我可以依赖从未写入过的 JavaScript 对象文字的键顺序吗?
- javascript - 如何在一定时间后执行代码php
- sql - 将工作表名称添加为要在 Google 表格中查询的列
- machine-learning - Transformer 是如何双向的 - 机器学习
- node.js - 使用 Redbird 反向代理 HTTPS 站点及其在 node.js 服务器上的子域
- ansible - Ansible:同时注册作业 ID 和退出代码
- mysql - 使用一组新记录更新数据库表,在 UI 显示中没有任何延迟
- c - 我们可以用另一种方式包含 C 源文件吗?
- laravel - 只保存 1 个单选按钮而不是 2 个
- laravel - 带有相关表的laravel登录身份验证