WPF内嵌WCF服务对外提供接口

要测试本帖子代码请记得管理员权限运行vs。html

我写这个帖子的初衷是在我作surface小车的时候有相似的需求,感受这个功能还挺有意思的,因此就分享给你们,网上有不少关于wcf的文章 我就不一一列举了。公司有个旧的项目是winform写的,里面就有这个内嵌的wcf,我还没怎么搞过wpf,你们都说winform太老了,因而乎我就想用wpf内嵌下试试看看能不能成功。git

下面是个人surface go小车的帖子。github

 http://www.javashuo.com/article/p-ebhtpbkm-er.htmlweb

 

这个项目我是参考网上的一个帖子。不过好多网友的贴子或多或少都有帮助,可是有的没收藏下来。我就贴上一个我以为是干货的帖子吧。浏览器

 .https://social.msdn.microsoft.com/forums/silverlight/en-US/d7afa073-e329-43a7-a120-7c59e1a4fd7f/how-to-return-html-page-from-wcf-with-webhttpbindingapp

首先咱们要确保vs里面安装了wcf组件。ide

如图即安装了桌面开发,又安装了wcf的组件就能够开始了。测试

管理员权限运行vs,首先新建一个wpf项目,这个你们应该都很熟悉。而后再在项目里添加新建项。spa

名称能够根据本身的业务命名。添加完成会在项目引用里多出一些dll文件。也会多出两个cs文件,那两个文件就是对外暴露的接口文件了,能够写一些业务逻辑给别人调用。orm

 

 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

相关文章
相关标签/搜索