要測試本帖子代碼請記得管理員權限運行vs。
我寫這個帖子的初衷是在我做surface小車的時候有類似的需求,感覺這個功能還挺有意思的,所以就分享給大家,網上有很多關於wcf的文章 我就不一一列舉了。公司有個舊的項目是winform寫的,里面就有這個內嵌的wcf,我還沒怎么搞過wpf,大家都說winform太老了,於是乎我就想用wpf內嵌下試試看看能不能成功。
下面是我的surface go小車的帖子。
https://www.cnblogs.com/GreenShade/p/10912738.html
這個項目我是參考網上的一個帖子。不過好多網友的貼子或多或少都有幫助,但是有的沒收藏下來。我就貼上一個我覺得是干貨的帖子吧。
首先我們要確保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
