我有个WCF服务没法运行下去,卡住了,请帮忙修改 英文为什么

下次自动登录
现在的位置:
& 综合 & 正文
IIS启动,就自动执行一个WCF服务方法的解决方案实现的疑问?
请教大家一个问题,下面是我当前项目中情况描述:
1:我有一个wcf服务方法:init(),里面实现的功能是创建一个线程,然后从远程主机一个端口取socket数据包。
2:这个方法我只需要执行一次,线程开启后就不需要再执行该方法了。
3:我现在解决方案是:把它写在页面加载中,当页面加载时,判断有没有创建该线程,如果没有则创建,有了就不在执行该方法。
问题出现了: 当项目发布到iis后,如果没有一个用户来访问,那么这个线程就永远也不会执行。这不是我期望的,我需要这个线程当iis启动时,就执行。
期望的解决方案:
当iis启动起来,那么这个init()服务方法得到执行。这样就不用写在页面的加载里面了,更不需要去判断。
希望大家帮我解决一下这个疑问谢谢
现在的模式就是在App_Start中去实现这个init方法。
1.定义为静态方法。
2.在够着函数里调用这个方法,单例模式。客户端开始的时候调用一次服务。
我现在的解决方案就是这样的。先客户端调用一次,之后用户请求,不再调用该方法。
但是现在问题是: 客户端调用是不受我们控制的,那是客户使用的。所以怎么样才能让第一次的客户端调用不让客户来进行。
可以考虑 给服务加个自定义 用户名 密码 验证,然后在 这里 调用 你 定义的 那个类的初始化方法
在IIS的进程模型中,如果没有访问请求,IIS不会为特定应用启动进程。
所以你必须在第一个请求到达你的服务的时候,去执行Init。
版主提供的方法都可以实现,个人推荐一个更好的方式,就是自定义ServiceHostFactory,在该类中实现调用Init()。
&&&&推荐文章:
【上篇】【下篇】Can not call WCF Service hosted in a Managed Windows Service [无法调用WCF服务托管在一个托管的Windows服务] - 问题-字节技术
Can not call WCF Service hosted in a Managed Windows Service
无法调用WCF服务托管在一个托管的Windows服务
问题 (Question)
So I have some Issue regarding a WCF Service that is hosted in a Managed Windows Service.
Basically what I did is the following:
I created a WCF Service Lib (the WCF Service template ) using a simple test, like this
[ServiceContract]
public interface IExample
[OperationContract]
string HelloWorld();
public class Example : IExample
public string HelloWorld()
return "HelloWorld";
I also created a corresponding app.config which is this
&?xml version="1.0" encoding="utf-8" ?&
&configuration&
&supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/&
&/startup&
&system.serviceModel&
&behaviors&
&serviceBehaviors&
&behavior name="ServiceBehavior"&
&serviceMetadata httpGetEnabled="true"/&
&/behavior&
&/serviceBehaviors&
&/behaviors&
&services&
&!-- This section is optional with the new configuration model introduced in .NET Framework 4. --&
&service name="Peripherie.WCFService" behaviorConfiguration="ServiceBehavior"&
&baseAddresses&
&add baseAddress="http://localhost:8067/PeripherieService"/&
&/baseAddresses&
&endpoint address="" binding="wsHttpBinding" contract="Peripherie.WCFService.Interfaces.IExample" /&
&endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /&
&/service&
&/services&
&/system.serviceModel&
&/configuration&
After that I added a Win Service project (again with the Win Service template) which references the above lib and the other needed libs.
In the Service class i do the basic stuff to create the servicehost
public partial class Service : ServiceBase
public ServiceHost serviceHost =
public Service()
InitializeComponent();
protected override void OnStart(string[] args)
if(serviceHost!=null)
serviceHost.Close();
serviceHost = new ServiceHost(typeof(Service));
serviceHost.Open();
protected override void OnStop()
if(serviceHost!=null)
serviceHost.Close();
serviceHost =
I also added the needed Installer for the Service and set the account to localSystem.
The whole project compiles just fine and I am also able to install the service (using the installutil approach) and start it as well. However when ever I try to open the the Service in the browser I get the error that the side could not be loaded, I am also not able to use the WCF Test Client as it tells me that there are is no metadata to be retrieved.
I dont really get why the whole think does not work, as it seems that everything is setup correctly.
So any advice would be nice.
After fixing the mistake pointed out by SouthShoreAK I also found an error in the config, where this:
&service name="Peripherie.WCFService" behaviorConfiguration="ServiceBehavior"&
should have been this:
&service name="Peripherie.WCFService.Services.Example" behaviorConfiguration="ServiceBehavior"&
Now I get the error that the url could not be registered,
System.ServiceModel.AddressAccessDeniedException: HTTP konnte URL "http://+:8067/PeripherieService/" nicht registrieren. Der Prozess weist keine Zugriffsrechte für diesen Namespace auf
I already tried the tool described
however that did not solve the error. Still can start the Service because of the error.
Okay that issue was resolved as well, I have the service process installer still set to networkService. After setting it to local system I can start the service now.
but still I get an error 400 now when calling the url via IE.
Final Edit:
Okay now it works, last error was because of a missing / at the end of the base address. So it should have been
&add baseAddress="http://localhost:8067/PeripherieService/"/&
And since SouthShoreAK pretty much pointed me towards the mistakes I made in my config I will accept his answer, because it got me on track.
所以我有一些问题对于一个主办的WCF服务在一个托管的Windows服务。基本上,我所做的是如下:我创建了一个WCF服务库(WCF服务模板)使用一个简单的测试,像这样[ServiceContract]
public interface IExample
[OperationContract]
string HelloWorld();
public class Example : IExample
public string HelloWorld()
return "HelloWorld";
我还创建了一个相应的app.config是这&?xml version="1.0" encoding="utf-8" ?&
&configuration&
&supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/&
&/startup&
&system.serviceModel&
&behaviors&
&serviceBehaviors&
&behavior name="ServiceBehavior"&
&serviceMetadata httpGetEnabled="true"/&
&/behavior&
&/serviceBehaviors&
&/behaviors&
&services&
&!-- This section is optional with the new configuration model introduced in .NET Framework 4. --&
&service name="Peripherie.WCFService" behaviorConfiguration="ServiceBehavior"&
&baseAddresses&
&add baseAddress="http://localhost:8067/PeripherieService"/&
&/baseAddresses&
&endpoint address="" binding="wsHttpBinding" contract="Peripherie.WCFService.Interfaces.IExample" /&
&endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /&
&/service&
&/services&
&/system.serviceModel&
&/configuration&
之后,我添加了一个赢得服务项目(再次赢得服务模板)参考上述库和其他所需要的库。在服务类,我做基本的东西创造ServiceHostpublic partial class Service : ServiceBase
public ServiceHost serviceHost =
public Service()
InitializeComponent();
protected override void OnStart(string[] args)
if(serviceHost!=null)
serviceHost.Close();
serviceHost = new ServiceHost(typeof(Service));
serviceHost.Open();
protected override void OnStop()
if(serviceHost!=null)
serviceHost.Close();
serviceHost =
我还添加了所需的安装和设置服务帐户LocalSystem。整个项目编译就好了,我还可以安装服务(使用installutil方法)以及启动它。然而,当我试图打开服务浏览器中我得到的错误,无法加载的一面,我也不能够使用WCF测试客户端就告诉我,有没有元数据检索。我真的明白为什么想全不工作,似乎一切都是正确的设置。所以任何的建议就好了。编辑固定的错误后,指出southshoreak我还发现了一个错误的配置,在这:&service name="Peripherie.WCFService" behaviorConfiguration="ServiceBehavior"&
应该是这样的:&service name="Peripherie.WCFService.Services.Example" behaviorConfiguration="ServiceBehavior"&
现在我得到错误,无法将URL注册,System.ServiceModel.AddressAccessDeniedException: HTTP konnte URL "http://+:8067/PeripherieService/" nicht registrieren. Der Prozess weist keine Zugriffsrechte für diesen Namespace auf
我已经尝试描述的工具然而,没有解决的错误。还可以启动服务,因为误差。编辑好了,问题解决的同时,我还将网络服务服务过程中的安装程序。在设置到本地系统,我可以现在开始服务。但我仍然得到错误400当调用URL通过IE浏览器。最后的编辑:好吧,现在工作,最后的错误是因为缺少/在底座的结束地址。所以它应该是&add baseAddress="http://localhost:8067/PeripherieService/"/&
由于southshoreak几乎指着我对我在我的配置,我将接受他的回答错误,因为它让我的轨道上。
最佳答案 (Best Answer)
Your contract should be IExample, not IMain
&endpoint name="ServiceHttpEndpoint" address="http://localhost:8067/PeripherieService" binding="wsHttpBinding" contract="Peripherie.WCFService.Interfaces.IMain" /&
Should be changed to:
&endpoint name="ServiceHttpEndpoint" address="http://localhost:8067/PeripherieService" binding="wsHttpBinding" contract="Peripherie.WCFService.Interfaces.IExample" /&
Also, this:
serviceHost = new ServiceHost(typeof(Service));
Should be this:
serviceHost = new ServiceHost(typeof(Example));
You're trying to register and instance of your Windows service in the service host. You should be registering your WCF service.
Sometimes I've found that your windows service will start and run even when the Service Host encounters an error. You may want to check your Windows Event Log (just type "Event Viewer" in the start menu) to see if anything went wrong.
你的合同应IExample,不imain &endpoint name="ServiceHttpEndpoint" address="http://localhost:8067/PeripherieService" binding="wsHttpBinding" contract="Peripherie.WCFService.Interfaces.IMain" /&
应改为: &endpoint name="ServiceHttpEndpoint" address="http://localhost:8067/PeripherieService" binding="wsHttpBinding" contract="Peripherie.WCFService.Interfaces.IExample" /&
同时,本:serviceHost = new ServiceHost(typeof(Service));
应该是这样的:serviceHost = new ServiceHost(typeof(Example));
你想注册您的Windows服务实例的服务主机。你应该注册您的WCF服务。有时我发现您的Windows服务将启动并运行,即使服务主机发生了一个错误。你可能要检查您的Windows事件日志(只要输入“事件查看器”中的“开始”菜单)是否出了什么差错。
本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:Is there something in WCF configuration that defines a timeout for executing a request at service side? E.g. WCF service will stop executing request after some time period. I have a service which make some work depending on client input. In some cases a such call may take too much time. I want to limit the execution time of such requests on service side, not client one using SendTimeout. I know about OperationTimeout property, but it doesn't abort the service request, it just tells a client that the request is timed out.
解决方案 In general terms, there's nothing that will totally enforce this. Unfortunately, it's one of those things that the runtime can't really enforce nicely without possibly leaving state messed up (pretty much the only alternative for it would be to abort the running thread, and that has a bunch of undesirable consequences).
So, basically, if this is something you want to actively enforce, it's a lot better to design your service to deal with this so that your operation execution has safe interruption points where the operation can be terminated if the maximum execution time has been exceeded.
Though it's a lot more work, you'll likely be more satisfied with it in the long run.
本文地址: &
在WCF配置中有什么定义了在服务端执行请求的超时吗?例如。 WCF服务将在一段时间后停止执行请求。我有一个服务,使一些工作取决于客户端输入。在某些情况下,此类呼叫可能需要很长时间。我想限制这种请求在服务端的执行时间,而不是客户端使用SendTimeout。我知道OperationTimeout属性,但它不会中止服务请求,它只是告诉客户端请求已超时。
解决方案 一般来说,没有什么会完全执行这个。不幸的是,它是那些事情之一,运行时不能真正强制执行,而不可能留下状态混乱(几乎唯一的替代方法是中止正在运行的线程,并有一堆不良后果)。
因此,基本上,如果这是你想要积极执行的东西,那么设计你的服务来处理这个问题会更好,以便你的操作执行有安全的中断点,如果超过了最大执行时间,则可以终止操作。
虽然这还有很多工作,但从长远来看,您可能会更满意。 / p>
本文地址: &
扫一扫关注官方微信wcf的服务端明明没有运行,但是状态竟然是opened? - 开源中国社区
当前访客身份:游客 [
当前位置:
共有0个回答
更多开发者职位上
有什么技术问题吗?
冬天之雪的其它问题
类似的话题温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
ServiceHost
前面使用 serviceType, params Uri[] baseAddresses)构造方法建立ServicesHost。
这里建立ServiceHost实例也必须使用代码,但是可以使用简单些的构造方法:
ServiceHost(typeof(Service))
只要给ServiceHost指定你要运行服务的类型,就是告诉要驻留在ServiceHost里的哪个WCF服务(实现某个或某些Contract的类)。BaseAddress部分这里不需要指定了,可以放在配置文件里。
编码部分就这些,剩下的部分都只要通过配置文件进行设置即可。
WCF的配置使用.NET Framework的System.Configuration配置系统。在Visual Studio中配置一个WCF服务时,如果宿主是一般的windows应用或console应用,则配置文件为App.confing,,如果宿主是IIS应用,则配置文件为Web.config。
跟WCF相关的配置主要有三个元素:
这三个元素都是.NET Framework配置文件的根元素下的元素,其中元素最为基本,WCF的基本设置集中在这个元素中。
看一下元素的简单结构:
&!--的配置要求
&!--和Endpiont行为
ServiceHost相配的service配置
前面已经在代码中建立了ServiceHost,并指定了这个ServiceHost中要驻留的服务类型。
在标签下加一个标签,注意,service的name属性应该是ServiceHost中驻留那个服务的全限定名,即名称空间加类名,表示这个service元素下的设置是针对这个服务的。
在元素下加元素,这里要增加的是baseAddress,跟使用代码一样,baseAddress可以有多个,用add标签增加,但是同样的通讯协议只能有一个baseAddress,比如http的只能有一个,https的只能有一个。
service增加Endpoint
本例中WCF服务对外只有一个Endpoint,在元素下增加元素,Endpoint的三个基本要素address、binding、contract都是元素属性,当然它还有其他属性,这里先不提及,只描述完成这个简单实例相关的设置。
address - 指定这个Endpoint对外的URI,这个URI可以是个绝对地址,也可以是个相对于baseAddress的相对地址。如果此属性为空,则这个Endpoint的地址就是baseAddress。
binding - 指定这个Endpoint使用的binding,这个banding可以是系统预定义的9个binding之一,比如是basicHttpBinding,也可以是自定义的customBinding。binding决定了通讯的类型、安全、如何编码、是否基于session、是否基于事务等等。
contract - 指定这个Endpoint对应的Contract的全限定名(名称空间.类型名),这个Contract应该被service元素的name指定的那个service实现。
&" name="WCFService.Service"&
&binding="basicHttpBinding" contract="WCFService.IService" /&
设置允许通过WSDL对外暴露对服务的Metadata。
是通过设置服务端行为的标签下增加一个,一个可以定义一组服务端的行为设置,可以设置一个或多个系统提供的或定制的表示服务端行为的元素。
Name属性,一个behavior唯一标识,&service&元素的behaviorConfiguration属性指向这个name,即表示这个service使用这个behavior的配置。
标签,指定service元数据发布和相关信息。
httpGetEnabled 属性是bool类型的值,表示是否允许通过HTTP的get方法获取sevice的WSDL元数据。
httpGetUrl 属性, 如果httpGetEnabled为true,这个属性指示使用哪个URL地址发布服务的WSDL,如果这个属性没有设置,则使用服务的HTTP类型的baseAddress后面加上?WSDL。
&" httpGetUrl="http://localhost:8001/" /&
客户端要访问服务端的服务,首先要知道服务端的服务提供了什么方法,就是要知道服务的Contract。如何取得服务端的Contract有几种方法,前一篇文章有讲述,这里不再赘述,这里同样采用最方便的在项目中添加Service reference来引用service。
在vs2005中安装了WCF的extention后,在项目的References上点击右键,会多出来一个“Add Service Reference”的选项,这就是用来引用WCF服务的,引用地址就是服务端设置的http的baseAddress。
在这里引用WCF服务,跟使用Svcutil.exe命令一样,会在项目中生成同样的两个文件。
service代理实例和配置文件
引用服务后,客户端生成了配置文件和包含了Contract和本地代理类的cs文件。
服务后,还会在同时给每个Contract不同的Endpoint生成一个继承自System.ServiceModel.ClientBase的本地代理类。
客户端可以直接使用多个重载的代理类构造方法实例化这些代理类。如果要使用配置文件,有这么几个构造方法可用:
endpointConfigurationName)
endpointConfigurationName, string remoteAddress)
endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress)
看一下,引用WCF后生成的配置文件中Client部分的内容,这部分包含客户端跟服务端连接使用到的Endpoint的配置:
" bindingConfiguration="BasicHttpBinding_IService"
" name="BasicHttpBinding_IService" /&
实际上,客户端代理类是从某个它对应的那个Contract继承的,所以客户端代理类本身一定是跟某个Contract相关的。
如果客户端配置文件中这个代理类对应的Contract只有一个Endpoint配置,那么可以使用第一个构造方法,运行时,会根据代理类的Contract在配置文件中相应的配置。
如果客户端配置文件这个代理类对应的Contract有多个Endpoint配置,可以使用第二个构造方法,通过参数指定使用哪一个Endpoint的配置。
对于本例,只有一个Contract,引用WCF服务后也只生成一个Endpoint配置,使用最简单的构造方法即可:
proxy = new localhost.ServiceClient();
使用配置文件的例子代码下载:
阅读(300)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_084075',
blogTitle:'使用配置文件构建和使用WCF服务',
blogAbstract:'当然,配置一个ServiceHost除了上面说的完全使用代码的方式,更好的方式是使用配置文件,把一些可能需要修改的属性跟代码分离,放到配置文件中,这样可以提供服务配置的灵活性,也更容易维护。\r\n看看前面那个不用配置文件的WCF',
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:0,
publishTime:3,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'',
hmcon:'0',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}}

我要回帖

更多关于 请帮忙催一下 英文 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信