这几年一直用WebApi较多,最近项目中有个需求比较适合使用WCF,以前也用过JQuery直接调用Wcf的,但是说实话真的忘了…

所以这次解决完还是花几分钟记录一下

WCF服务端:宿主在现有Win服务中,在服务启动时添加代码 ,服务代码就不用写了,接口和实现按照契约实现即可

           private ServiceHost serviceHost = null; //服务宿主

        //启动WCF服务
if (serviceHost != null)
{
serviceHost.Close();
}
serviceHost = new ServiceHost(typeof(ControlService));
serviceHost.Open();
Toolkit.Log.Warn("WCF服务启动");

服务端ServiceModel配置:个人感觉也是最麻烦的地方,WCF本身支持多种通信方式,所以配置较多,不过基本了解后也差不多

详细可参考:http://www.cnblogs.com/ingstyle/p/5711249.html

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<!-- This section is optional with the new configuration model
introduced in .NET Framework . -->
<service name="命名空间+类名"
behaviorConfiguration="ControlServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/ServiceModel/service.svc"/>
</baseAddresses>
</host>
<!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/ServiceModel/service -->
<endpoint address=""
binding="wsHttpBinding"
contract="命名空间+接口名" />
<!-- the mex endpoint is exposed at http://localhost:8000/ServiceModel/service/mex -->
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ControlServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

启动完后可访问:http://localhost:8000/ServiceModel/service.svc 查看如下图:

客户端WCF服务调用封装:

public class WCFHelper
{
public WCFHelper()
{ } public static object ExecuteMethod<T>(Binding bind, string pUrl, string pMethodName, params object[] pParams)
{
EndpointAddress address = new EndpointAddress(pUrl);
Binding bindinginstance = null;
bindinginstance = bind;
using (ChannelFactory<T> channel = new ChannelFactory<T>(bindinginstance, address))
{
T instance = channel.CreateChannel();
using (instance as IDisposable)
{
try
{
Type type = typeof(T);
MethodInfo mi = type.GetMethod(pMethodName);
return mi.Invoke(instance, pParams);
}
catch (TimeoutException)
{
(instance as ICommunicationObject).Abort();
throw;
}
catch (CommunicationException)
{
(instance as ICommunicationObject).Abort();
throw;
}
catch (Exception vErr)
{
(instance as ICommunicationObject).Abort();
throw;
}
}
}
}
}

由于是动态调用的,所以没有任何配置,可以向下边这样直接使用:

        WSHttpBinding bind = new WSHttpBinding();//重点这儿,可以传递不同的Bind,和对应的服务端配置参数(如果参数和服务端不一致则会报错)
WCFHelper.ExecuteMethod<IControlService>(bind,"http://localhost:8000/ServiceModel/service.svc", "ResetTerminal", new object[] { "参数" });

注意第一个参数需根据对应的WCF服务端配置对应

例如服务端是webHttpBinding,这儿也换成webHttpBinding bind=new webHttpBinding() 即可,这里我使用的WSHttpBinding

如果有想了解JQuery调用WCF的方式请参考站长的博客:http://www.cnblogs.com/dudu/archive/2009/07/14/1523082.html

注意:如果碰到 "响应消息的内容类型 text/html; charset=utf-8 与绑定(text/xml; charset=utf-8)的内容类型不匹配。" 

也有可能是Bind的方式不匹配,另外Host里的地址尽量用IP地址来绑定

"响应消息的内容类型 text/html; charset=utf-8 与绑定(text/xml; charset=utf-8)的内容类型不匹配。"问题的解决办法

最新文章

  1. 第十三章:降维:主成分分析PCA
  2. 2016 - 1- 22 Build a Nav bar (intro to HTML&amp;CSS)
  3. HDU 3586 Information Disturbing (二分+树形dp)
  4. 搜狐云景paas平台实践之路
  5. art template前端模板引擎
  6. 【USACO 1.4.2】时钟
  7. VS2010 C#调用C++ DLL文件
  8. 【转载】LINUX上MYSQL优化三板斧
  9. 02_3中方式的反射,通过Class.forName获得Class对象,通过类.class获得字节码对象,通过类实例.getClass()的方式获得Class对象
  10. kafka 创建消费者报错 consumer zookeeper is not a recognized option
  11. [转]Ble蓝牙的使用手册
  12. *args **kwargs
  13. Android-View的绘制源码学习总结
  14. Failed to execute &#39;setRequestHeader&#39; on &#39;XMLHttpRequest&#39;: The object&#39;s state must be OPENED.
  15. Python解释器种类以及特点 (经典概括, 便于理解和记忆)
  16. OC中数组排序总结
  17. angular5新增全局的模块
  18. MMORPG 游戏服务器端设计
  19. bootstrap基础学习九篇
  20. Lambda中的常用sql方法

热门文章

  1. 同步锁Lock
  2. 025——VUE中事件的基本使用与VUE中差异
  3. linux系统参数统计脚本
  4. C++面向对象高级编程(六)转换函数与non-explicit one argument ctor
  5. An error report file with more information is saved as hs_err_pid2756.log
  6. JAVA交换两个变量的值-(不使用第三变量)
  7. IOS开发 多线程编程 - NSOperation
  8. Android使用HTTP协议访问网络——HttpClient
  9. 如何让Beamer的logo放在右上角
  10. ueditor使用小结【来源网络】