服务端配置

<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name ="WsHttpBinding_IService" maxReceivedMessageSize="370000" receiveTimeout="00:10:01" maxBufferPoolSize="100">
<readerQuotas maxStringContentLength="240000"/>
<security mode="Transport">
<transport clientCredentialType="Windows"></transport>
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="WCF_Find_Error_Lib.Service" behaviorConfiguration="beh">
<endpoint address=""
binding="wsHttpBinding"
contract="WCF_Find_Error_Lib.IService"
bindingConfiguration="WsHttpBinding_IService">
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost/S" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="beh">
<serviceThrottling maxConcurrentCalls="1"/>
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" httpGetUrl="http://localhost/S"/>
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>

服务契约

[ServiceContract]
public interface IService
{
[OperationContract]
string GetData(int value); [OperationContract]
string GetString(string value); [OperationContract]
void Upload(Request request);
} [MessageContract]
public class Request
{
[MessageHeader(MustUnderstand = true)]
public string FileName { get; set; } [MessageBodyMember(Order = )]
public Stream Content {get;set;}
}

服务

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class Service : IService
{
public string GetData(int value)
{
//Thread.Sleep(120000);
return string.Format("You entered: {0}", value);
} public string GetString(string value)
{
//Thread.Sleep(120000);
Thread.Sleep();
return string.Format("You entered: {0}", value);
} public void Upload(Request request)
{
try
{
StreamReader sr = new StreamReader(request.Content, Encoding.GetEncoding("GB2312"));
StreamWriter sw = new StreamWriter("E:\\" + request.FileName + ".txt", false, Encoding.GetEncoding("GB2312"));
while (!sr.EndOfStream)
{
sw.WriteLine(sr.ReadLine());
Thread.Sleep();
}
sr.Close();
sw.Close();
}
catch (Exception ex)
{ } }
  }

服务寄宿

  class Program
{
static void Main(string[] args)
{
try
{
ServiceHost host = new ServiceHost(typeof(Service));
host.Open();
Console.WriteLine("服务状态:"+host.State.ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
} Console.WriteLine("服务启动");
Console.WriteLine("按任意键停止服务");
Console.ReadLine();
}
  }

客户端配置

<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name ="WsHttpBinding_IService" maxReceivedMessageSize="370000" receiveTimeout="00:10:01" maxBufferPoolSize="100">
<readerQuotas maxStringContentLength="240000"/>
<security mode="Transport">
<transport clientCredentialType="Windows">
</transport>
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost/S" binding="wsHttpBinding"
bindingConfiguration="WsHttpBinding_IService" contract="IService"
name="WHttpBinding_IService" />
</client>
</system.serviceModel>

客户端代理

  public class ServiceProxy
{
public string GetData(int value)
{
string ret = null;
ServiceClient client = null;
try
{
client = new ServiceClient();
ret = client.GetData(value);
client.Close();
}
catch
{
if (client != null)
{
client.Abort();
}
throw;
}
return ret;
} public string GetString(string value)
{
string ret = null;
ServiceClient client = null;
try
{
client = new ServiceClient();
ret = client.GetString(value);
client.Close();
}
catch
{
if (client != null)
{
client.Abort();
}
throw;
}
return ret;
}
public void Upload(Request request)
{
ServiceClient client = null;
try
{
client = new ServiceClient();
client.Upload(request);
client.Close();
}
catch
{
if (client != null)
{
client.Abort();
}
throw;
}
} } [ServiceContractAttribute(ConfigurationName = "IService")]
public interface IService
{ [System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IService/GetData", ReplyAction = "http://tempuri.org/IService/GetDataResponse")]
string GetData(int value); [System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IService/GetString", ReplyAction = "http://tempuri.org/IService/GetStringResponse")]
string GetString(string value); [System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IService/Upload", ReplyAction = "http://tempuri.org/IService/UploadResponse")]
void Upload(Request request);
}
[MessageContract]
public class Request
{
[MessageHeader(MustUnderstand = true)]
public string FileName { get; set; } [MessageBodyMember(Order = )]
public Stream Content { get; set; }
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class ServiceClient : System.ServiceModel.ClientBase<IService>, IService
{ public ServiceClient()
{
} public string GetData(int value)
{
return base.Channel.GetData(value);
} public string GetString(string value)
{
return base.Channel.GetString(value);
} public void Upload(Request request)
{
base.Channel.Upload(request);
}
  }

1 Transport安全模式(本机调试)

 客户端调用

using (ServiceClient client = new ServiceClient())
{
StreamReader sr = new StreamReader("D:\\CSBMTEMP.txt", Encoding.Default);
string str = sr.ReadToEnd();
sr.Close();
var s = client.GetString(str);
}

服务端和客户端配置如上。wsHttpBinding的Message安全模式,客户端凭据默认为Windows

运行客户端,抛出异常:

抛出异常的原因是,Transport模式支持HTTPS,TCP,ICP,MSMQ,而这里终结点地址是http://localhost/S,没有使用HTTPS协议。将终结点地址改为https://localhost/S即可。

服务端和客户端配置的安全模式不一样时

服务端安全模式修改为:

<security mode="Transport">
<transport clientCredentialType="Basic"></transport>
</security>

客户端保持不变:

<security mode="Transport">
<transport clientCredentialType="Windows"></transport>
</security>

运行客户端,抛出异常:

将客户端安全配置改为Basic,与服务端相同

<security mode="Transport">
<transport clientCredentialType="Basic"></transport>
</security>

运行客户端,抛出异常,因为Transport模式不支持Basic这种客户端凭据。

2 Message安全模式(本机调试)

 wsHttpBinding的Message安全模式,客户端凭据默认为Windows。

服务地址配置为https://localhost/S,服务端与客户端安全模式相同

<security mode="Message">
<transport clientCredentialType="Windows"></transport>
</security>

运行客户端,抛出异常

抛出异常的原因是,wsHttpBinding的Message安全模式不支持https协议,改为http协议则正常。

但是,当服务端的客户端凭据配置与客户端不一致时,也可以正常执行,并获得正常的结果。

例如,服务端配置:

<security mode="Message">
<transport clientCredentialType="Basic"></transport>
</security>

客户端配置:

<security mode="Message">
<transport clientCredentialType="Windows"></transport>
</security>

-----------------------------------------------------------------------------------------

转载与引用请注明出处。

时间仓促,水平有限,如有不当之处,欢迎指正。

学会WCF之试错法——安全配置报错分析的更多相关文章

  1. 解决eclipse spring配置报错:cvc-elt.1: Cannot find the declaration of element

    解决eclipse spring配置报错:cvc-elt.1: Cannot find the declaration of element 'beans'.Referenced file conta ...

  2. const变量赋值报错分析

    const变量赋值报错分析 const变量赋值报错 从变量到常量的赋值是合法C++的语法约定的, 如从char 到const char顺畅: 但从char **到 const char **编译器就会 ...

  3. mysql5.7密码修改与报错分析

    1.修改密码 修改密码: vim /etc/my.cnf 的mysqld字段加入skip-grant-tables 重启MySQL,service mysqld restart 终端输入 mysql ...

  4. std::unique_ptr使用incomplete type的报错分析和解决

    Pimpl(Pointer to implementation)很多同学都不陌生,但是从原始指针升级到C++11的独占指针std::unique_ptr时,会遇到一个incomplete type的报 ...

  5. rsyslog配置报错解决

    配置过程中,查看/var/log/meassage 有报错信息: action '*' treated as ':omusrmsg:*' - please use ':omusrmsg:*' synt ...

  6. 由登录服务器时ulimit配置报错,也谈下ulimit配置

    最近在登录开发机时,有报错如下: -bash: cannot modify limit: Operation not permitted 一定是哪个地方有ulimit设置,想想看,用户登录或用户su命 ...

  7. maven 配置报错 JAVA_HOME not found in your environment

    最近比较空,想研究下spring mvc,于是编按照教程一步一步配置开发环境.配置maven完成后,运行命令mvn -v的时候,竟然报错.错误信息如下: Error: JAVA_HOME not fo ...

  8. 学会WCF之试错法——数据传输

    数据传输 服务契约 [ServiceContract] public interface IService { [OperationContract] string GetData(int value ...

  9. webpack配置报错:invalid configuration object.webpack has been initialisted using a configuration objcet that does not match thie API schema

    最近接收了别人的项目,webpack配置总是报错如下:最后找到了解决办法,在此分享一下: 错误情况: 解决办法: 将package.json里面的colors删除掉即可

随机推荐

  1. ASP.NET没有魔法——ASP.NET MVC 与数据库之MySQL

    之前介绍了My Blog如何使用ADO.NET来访问SQL Server获取数据.本章将介绍如何使用My SQL来完成数据管理. 在使用My SQL之前需确保开发环境中安装了My SQL数据库和Con ...

  2. Unity3D - Animator Controller循环依赖

    问题 假设有2个Animator Controller,分别命名为TestControllerLhs.controller以及TestControllerRhs.controller.在TestCon ...

  3. 初学者易上手的SSH-struts2 04值栈与ognl表达式

    什么是值栈?struts2里面本身提供的一种存储机制,类似于域对象,值栈,可以存值和取值.,特点:先进后出.如果将它当做一个容器的话,而这个容器有两个元素,那么最上面的元素叫做栈顶元素,也就是所说的压 ...

  4. 邮件实现详解(四)------JavaMail 发送(带图片和附件)和接收邮件

    好了,进入这个系列教程最主要的步骤了,前面邮件的理论知识我们都了解了,那么这篇博客我们将用代码完成邮件的发送.这在实际项目中应用的非常广泛,比如注册需要发送邮件进行账号激活,再比如OA项目中利用邮件进 ...

  5. Android模拟器检测常用方法

    在Android开发过程中,防作弊一直是老生常谈的问题,而模拟器的检测往往是防作弊中的重要一环,接下来有关于模拟器的检测方法,和大家进行一个简单的分享. 1.传统的检测方法. 传统的检测方法主要是对模 ...

  6. 微信公众号第三方 推送component_verify_ticket协议

    整了一天,终于弄明白了 component_verify_ticket 怎么获取的了.在此先批一下微信公众号平台,文档又没写清楚,又没有客服,想搞哪样哈! 好,回归正题. 第一,先通过开发者资质认证, ...

  7. c语言构造类型之数组_01

    构造类型--constructed type.至于定义,笔者就省略了,有兴趣的同学可以百度搜索https://www.baidu.com/.今天我们要说的是c语言中最简单的构造类型--数组(array ...

  8. Windows 10 16251 添加的 api

    本文主要讲微软最新的sdk添加的功能,暂时还不能下载,到 7月29 ,现在可以下载是 16232 ,支持Neon效果 实际上设置软件最低版本为 16232 就自动支持 Neon 效果. 主要添加了 A ...

  9. Mongodb 认证鉴权那点事

    [TOC] 一.Mongodb 的权限管理 认识权限管理,说明主要概念及关系 与大多数数据库一样,Mongodb同样提供了一套权限管理机制. 为了体验Mongodb 的权限管理,我们找一台已经安装好的 ...

  10. Javascript中NaN、null和undefinded的区别

    var a1; var a2 = true; var a3 = 1; var a4 = "Hello"; var a5 = new Object(); var a6 = null; ...