要测试本帖子代码请记得管理员权限运行vs。
我写这个帖子的初衷是在我做surface小车的时候有类似的需求,感觉这个功能还挺有意思的,所以就分享给大家,网上有很多关于wcf的文章 我就不一一列举了。公司有个旧的项目是winform写的,里面就有这个内嵌的wcf,我还没怎么搞过wpf,大家都说winform太老了,于是乎我就想用wpf内嵌下试试看看能不能成功。
下面是我的surface go小车的帖子。
https://www.cnblogs.com/greenshade/p/10912738.html
这个项目我是参考网上的一个帖子。不过好多网友的贴子或多或少都有帮助,但是有的没收藏下来。我就贴上一个我觉得是干货的帖子吧。
.https://social.msdn.microsoft.com/forums/silverlight/en-us/d7afa073-e329-43a7-a120-7c59e1a4fd7f/how-to-return-html-page-from-wcf-with-webhttpbinding
首先我们要确保vs里面安装了wcf组件。
如图即安装了桌面开发,又安装了wcf的组件就可以开始了。
管理员权限运行vs,首先新建一个wpf项目,这个大家应该都很熟悉。然后再在项目里添加新建项。
名称可以根据自己的业务命名。添加完成会在项目引用里多出一些dll文件。也会多出两个cs文件,那两个文件就是对外暴露的接口文件了,可以写一些业务逻辑给别人调用。
servicemodel就是比较主要的dll,我们用的一些服务都是这里面的。下面的servicemodel.web是我手动添加的。
同时项目里面的app.config配置文件会出现wcf相关的配置。
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedruntime version="v4.0" sku=".netframework,version=v4.7.2" /> </startup> <system.servicemodel> <behaviors> <servicebehaviors> <behavior name=""> <servicemetadata httpgetenabled="true" httpsgetenabled="true" /> <servicedebug includeexceptiondetailinfaults="false" /> </behavior> </servicebehaviors> </behaviors> <services> <service name="wpfwcf.service1"> <endpoint address="" binding="basichttpbinding" contract="wpfwcf.iservice1"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexhttpbinding" contract="imetadataexchange" /> <host> <baseaddresses> <add baseaddress="http://localhost:8733/design_time_addresses/wpfwcf/service1/" /> </baseaddresses> </host> </service> </services> </system.servicemodel> </configuration>
我是将system.servicemodel节点的相关东西都删掉了。然后换成了之前在网上找到的配置方法。大家可以直接拿来使用。主要是把 service name和conract相关的改成自己的项目的就行了。
<system.servicemodel> <services> <service name="wpftestwcf.service1" behaviorconfiguration="default"> <endpoint address="" behaviorconfiguration="webhttp" binding="webhttpbinding" bindingconfiguration="general" contract="wpftestwcf.iservice1"> <identity> <dns value="localhost"/> </identity> </endpoint> <endpoint address="mex" binding="mexhttpbinding" contract="imetadataexchange"/> <host> <baseaddresses> <add baseaddress="http://localhost:8733/service1"/> </baseaddresses> </host> </service> </services> <behaviors> <endpointbehaviors> <behavior name="webhttp"> <webhttp/> </behavior> </endpointbehaviors> <servicebehaviors> <behavior name="default"> <servicemetadata httpgetenabled="true"/> <servicethrottling maxconcurrentcalls="256" maxconcurrentsessions="1024" maxconcurrentinstances="1024"/> <servicedebug includeexceptiondetailinfaults="true"/> </behavior> </servicebehaviors> </behaviors> <bindings> <webhttpbinding> <binding name="general" closetimeout="00:01:00" opentimeout="00:01:00" receivetimeout="00:01:00" sendtimeout="00:01:00" maxreceivedmessagesize="4194304" maxbuffersize="4194304" maxbufferpoolsize="33554432"> <readerquotas maxdepth="32" maxarraylength="16384" maxstringcontentlength="16384" maxbytesperread="8192" maxnametablecharcount="65536"/> <security mode="none"> <transport clientcredentialtype="none"/> </security> </binding> </webhttpbinding> </bindings> </system.servicemodel>
此时配置相关的算是已经配置好了。然后我们就需要启动服务和写接口了。我们在主窗口放一个button然后搞个点击事件。
事件里就写上启动服务的代码。
private void button_click(object sender, routedeventargs e) { servicehost host = new servicehost(typeof(service1)); host.open(); }
记得关闭的时候释放下。
下面是接口相关。我们开始添加wcf服务的时候引入了两个主要的dll。
下面是我的接口名称定制部分。
using system; using system.collections.generic; using system.linq; using system.runtime.serialization; using system.servicemodel; using system.servicemodel.web; using system.text; namespace wpftestwcf { // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“iservice1”。 [servicecontract] public interface iservice1 { [operationcontract, webget(uritemplate = "test?name={filename}", responseformat = webmessageformat.json)] string dowork(string filename); } }
实现就是return filename,就是测试数据。
点击按钮启动服务。然后通过浏览器调用在app.config里定义好的服务地址,就可以调用到我们接口里的方法了,然后就可以像调用web服务一样了。
项目代码我就不上传了。已经讲的很详细了。我已经把代码整合到我的surface go项目里了,下面是github的地址。
https://github.com/greenshadezhang/greenshade.net