宿主在Windows Service中的WCF(创建,安装,调用) (host到exe,非IIS)
在vs2010中创建WCF服务应用程序,会自动生成一个接口和一个实现类:(IService1和Service1)
IService1接口如下:
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.ServiceModel.Web;
- using System.Text;
- namespace WcfService
- {
- [ServiceContract]
- public interface IService1
- {
- [OperationContract]
- string GetData(int value);
- [OperationContract]
- CompositeType GetDataUsingDataContract(CompositeType composite);
- }
- // 使用下面示例中说明的数据约定将复合类型添加到服务操作。
- [DataContract]
- public class CompositeType
- {
- bool boolValue = true;
- string stringValue = "Hello ";
- [DataMember]
- public bool BoolValue
- {
- get { return boolValue; }
- set { boolValue = value; }
- }
- [DataMember]
- public string StringValue
- {
- get { return stringValue; }
- set { stringValue = value; }
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.ServiceModel.Web;
- using System.Text;
- namespace WcfService
- {
- public class Service1 : IService1
- {
- public string GetData(int value)
- {
- return string.Format("You entered: {0}", value);
- }
- public CompositeType GetDataUsingDataContract(CompositeType composite)
- {
- if (composite == null)
- {
- throw new ArgumentNullException("composite");
- }
- if (composite.BoolValue)
- {
- composite.StringValue += "Suffix";
- }
- return composite;
- }
- }
- }
先在window Service中添加引用,在对话框中选择Projects->Solution然后将wcfservice引入,这就在windows service中引用wcfservice里的service1时就不会报错了。
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.Linq;
- using System.ServiceProcess;
- using System.Text;
- using System.ServiceModel;
- using WcfService;
- namespace WindowsServiceDemo
- {
- public partial class Baowg : ServiceBase
- {
- private ServiceHost host;
- public Baowg()
- {
- InitializeComponent();
- }
- protected override void OnStart(string[] args)
- {
- if (this.host != null)
- {
- this.host.Close();
- }
- this.host = new ServiceHost(typeof(WcfService.Service1));
- this.host.Open();
- }
- protected override void OnStop()
- {
- if (this.host != null)
- {
- this.host.Close();
- }
- }
- }
- }
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <system.serviceModel>
- <services>
- <service name="WcfService.Service1" behaviorConfiguration="basicBehavior">
- <host>
- <baseAddresses>
- <add baseAddress="http://localhost:8999/Baowg"/> <!--windows service的地址-->
- </baseAddresses>
- </host>
- <!--wcfservice的地址-->
- <endpoint address="http://localhost:8999/Service1" contract="WcfService.IService1" binding="basicHttpBinding" />
- </service>
- </services>
- <behaviors>
- <serviceBehaviors>
- <behavior name="basicBehavior">
- <serviceMetadata httpGetEnabled="true" />
- </behavior>
- </serviceBehaviors>
- </behaviors>
- </system.serviceModel>
- </configuration>
在设计面板上有两个控件:
一个叫serviceProcessInstaller1.选中它,到属性窗口,选择account,可以选择windows servcie的login用户身份,一般选择NetworkService.
一个叫ServiceInstaller1.选中它到属性窗口,可以设置服务名,启动类型等关于服务的一些设置。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8999/Service1" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>
</configuration>
宿主在Windows Service中的WCF(创建,安装,调用) (host到exe,非IIS)的更多相关文章
- NetCore Selfhost,IIShost,Windows Service Host详解(自宿主、宿主在IIS,宿主在Windows Service中)
第一部分.自托管 一.依赖.Net Core环境 修改 project.json 文件内容,增加发布时需要包含文件的配置内容(NetCore2.0版本不需要任何设置,NetCore2.0开始彻底放弃p ...
- 如何托管ASP.NET Core应用到Windows Service中
(此文章同时发表在本人微信公众号"dotNET开发经验谈",欢迎右边二维码来关注.) 题记:正在构思一个中间件的设计,考虑是否既可以使用最新的技术,也可以兼顾传统的部署模式.所以有 ...
- ASP.NET Core应用到Windows Service中
托管到Windows Service中 众所周知,ASP.NET Core采用了和传统ASP.NET不同的托管和HTTP处理方式,即把服务器和托管环境完全解耦. ASP.NET Core内置了两个HT ...
- .NET 6学习笔记(3)——在Windows Service中托管ASP.NET Core并指定端口
在上一篇<.NET 6学习笔记(2)--通过Worker Service创建Windows Service>中,我们讨论了.NET Core 3.1或更新版本如何创建Windows Ser ...
- Windows Service中使用Threading.Timer需注意回收
在Windows Service中使用Threading.Timer时需要注意回收池问题 Threading.Timer是基于线程池的,系统会对其进行垃圾回收. 当Threading.Timer定义在 ...
- C/C++中动态链接库的创建和调用
DLL 有助于共享数据和资源.多个应用程序可同时访问内存中单个DLL 副本的内容.DLL 是一个包含可由多个程序同时使用的代码和数据的库.下面为你介绍C/C++中动态链接库的创建和调用. 动态连接库的 ...
- [转贴] C/C++中动态链接库的创建和调用
DLL 有助于共享数据和资源.多个应用程序可同时访问内存中单个DLL 副本的内容.DLL 是一个包含可由多个程序同时使用的代码和数据的库.下面为你介绍C/C++中动态链接库的创建和调用. 动态连接库的 ...
- windows 10中的ubuntu子系统安装桌面环境的方法
windows 10中的ubuntu子系统安装桌面环境的方法 (How to install Ubuntu-desktop in windows 10 Subsystem for Linux) 转载 ...
- 创建寄宿在Windows服务中的WCF服务
1.创建Windows服务项目 2.Server1改名为你想要的名称,比如WinServer 3.在项目中新建一个WCF文件夹,用于存放wcf服务文件. 注:在WcfServer类的上面还要添加 [S ...
随机推荐
- systemd管理服务
[root@zbs-staging-api system]# cat /lib/systemd/system/ncmulti@.service [Unit] Description=many on % ...
- C 中结构体对齐
参考 百度百科内存对齐 对齐作用 可以使得以最少的次数将操作数加载到寄存器中,如果数据没有对齐,则当CPU以最小读取数据大小从内存读入数据时可能只取到了一部分数据,而对齐情况下可以一次读入. 对齐修改 ...
- python 约束,异常处理与MD5加密
1. 类的约束 1. 写一个父类. 父类中的某个方法要抛出一个异常 NotImplementedError (重点) 2. 抽象类和抽象方法 # 语法 # from abc import ABCMet ...
- form图片上传遇到错误
org.springframework.web.multipart.MultipartException: The current request is not a multipart request ...
- cf711D. Directed Roads(环)
题意 题目链接 \(n\)个点\(n\)条边的图,有多少种方法给边定向后没有环 Sol 一开始傻了,以为只有一个环...实际上N个点N条边还可能是基环树森林.. 做法挺显然的:找出所有的环,设第\(i ...
- 洛谷P2881 [USACO07MAR]排名的牛Ranking the Cows(bitset Floyd)
题意 题目链接 Sol 显然如果题目什么都不说的话需要\(\frac{n * (n - 1)}{2}\)个相对关系 然后求一下传递闭包减掉就行了 #include<bits/stdc++.h&g ...
- AJAX 简单归纳 -- 前端知识
什么是 AJAX ? AJAX = 异步 JavaScript 和 XML. AJAX 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味 ...
- 【代码笔记】iOS-removeFromSuper
代码: RootViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after ...
- JS 显示隐藏DIV
JS关闭DIV HTML <div id="bar1"> <p onclick="removeElement('bar1')">关闭&l ...
- IIS 下调用证书出现异常解决方案 (C#)
程序发布前,跑在vs上是没问题的,当发布后,程序就报错了.通过系统日志找到了错误所在:证书调用时出现了异常.原因是:在IIS上调用证书是需要配置的,具体配置如下: 一. 确保证书已安装 1. 点击 [ ...