记录:Web无引用无配置方式动态调用WCF服务

时间:2021-06-30 18:55:34

这几年一直用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 查看如下图:

记录:Web无引用无配置方式动态调用WCF服务

客户端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)的内容类型不匹配。"问题的解决办法