要测试本帖子代码请记得管理员权限运行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

												

最新文章

  1. android的消息处理机制——Looper,Handler,Message
  2. Web APi 2.0优点和特点?在Web APi中如何启动Session状态?
  3. gulp基本介绍
  4. Fiddler响应post的请求 request body里面填写什么?
  5. DB2中ixf文件的导入导出
  6. Linux内核--usb子系统的分析
  7. [反汇编练习] 160个CrackMe之007
  8. 让WPF的Popup不总置顶的解决方案
  9. IDEA - Project files cannot be watched (are they under network mount?)
  10. .net调用Outlook 批量发送邮件,可指定Outlook中的账号来发送邮件
  11. 微信小程序 问题收集
  12. Django配置mysql
  13. rails无法使用页面缓存的解决办法
  14. 痞子衡嵌入式:飞思卡尔i.MX RT系列MCU特性介绍(3)- 命名规则
  15. 进程间通信之——队列Queue
  16. 通过谷歌浏览器,找到页面某个事件属于哪个js文件
  17. xcode 调试器 LLDB
  18. 20155211 2016-2017-2 《Java程序设计》第3周学习总结
  19. STM32(12)——CAN
  20. linux 基础知识总结

热门文章

  1. CSS布局总结(二)
  2. [luogu4133 BJOI2012] 最多的方案 (计数dp)
  3. [读书笔记] R语言实战 (五) 高级数据管理
  4. Vue 做项目经验
  5. http响应的封装
  6. windows server 2008开机自动登陆无密码,关机不必写原因
  7. HDU 4259
  8. [ES2018] Two ways to write for-await-of
  9. 外网主机如何将数据包发送到共用一个公网IP的局域网某特定主机上的
  10. python设计模式 之 简单工厂模式