如何访问有关 WCF 服务部署位置的信息
我在 IIS7 中托管的多个网站中部署了一项 WCF 服务,有时在 VS2010 中托管(开发环境)。
通常多个实例连接到同一个数据库,通常它的只读访问不是问题,但有时(例如在日志记录中)我需要唯一标识访问数据库的服务实例。
目前,我通过在 web.config 中添加特殊属性来实现此目的,稍后我将其用作主键的一部分,但我确实觉得它不是理想的解决方案。
我考虑了站点名称(如果托管在 IIS 中)和计算机名称 + 端口(如果托管在 VS2010 中)。
问题来了:如何访问有关 WCF 服务部署位置的信息以及如何从 C# 中的已部署服务内部以编程方式执行此操作?这有可能吗?
感谢您的建议。 米哈尔
I have one WCF service deployed in multiple websites hosted in IIS7 and sometimes (development environemnt) in VS2010.
Often multiple instances are connected to the same database and usually its not a problem as its read-only access, but sometimes (like in logging) I need to uniquely identify instance of the service accessing DB.
Currently I do it by putting special attribute in web.config which I'll later use as a part of primary key, but I do feel its not ideal solution.
I thought about site-name in case its hosted in IIS and machine name + port if its hosted from VS2010.
And here comes the question: How to access info about where the WCF service is deployed and how to do it programmatically from inside of deployed service in c#? Is it at all possible?
Thank you for your suggestions.
Michal
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
前几天有一个关于如何获取WCF服务器的IP地址以进行日志记录的问题。本主题可能会有所帮助:如何获取 IP 地址WCF Web 服务的
您还可以通过找出您的应用程序正在使用哪个进程运行来确定您是否在 IIS 中运行
System.Diagnostics.Process.GetCurrentProcess()
,并查看它是否是“w3svc.exe”,这是 IIS 应用程序池运行的内容。The other day there was a question about how to get the IP address of the WCF server, for logging. This topic might help: How to get IP address of WCF web service
You could also determine if you are running in IIS by finding out what process your app is being run from using
System.Diagnostics.Process.GetCurrentProcess()
, and see if it is "w3svc.exe", which is what the IIS app pool runs as.如果我正确理解您的问题(可能不会),您想知道实际实例部署在服务器上。
如果是这种情况,您可以使用类似的方法:
或者我相信这也会起作用:
在程序集或进程本身上使用反射将不起作用,因为正在运行的进程是主机(IIS、IISExpress 等),但是我的两种方法指定的应该为您提供托管应用程序服务本身的位置。
If I understand your question correctly, and I may not, you would like to know were on the server the actual instance is deployed.
If this is the case you can use something like:
Or I believe this will also work:
Using reflection on the assembly or process itself will not work because the running process is the host (IIS, IISExpress, etc), but the two methods I specified should give you the location of the service itself for hosted apps.