简体   繁体   English

Web服务的配置

[英]Configuration for Web Service

I have the WCF application with Web Service. 我有带有Web服务的WCF应用程序。 And I have console application to host Web Service: 我有控制台应用程序来承载Web Service:

 ServiceHost host = new ServiceHost(typeof(MyService));
 host.Open();

The Web Service connect to the database and I want to store the connection string in the configuration file to allow edit it without solution recompile. Web服务连接到数据库,我想将连接字符串存储在配置文件中,以允许对其进行编辑而无需重新编译解决方案。

I can add this connection string to App.config of web service, but when I build the Console Hosting Application it will have your own App.config file. 我可以将此连接字符串添加到Web服务的App.config,但是当我构建控制台宿主应用程序时,它将具有您自己的App.config文件。

I can add the connection string to App.config of Console Host Application, but I don't know how to pass the parameter from Hosting Application to Web Service. 我可以将连接字符串添加到控制台主机应用程序的App.config中,但是我不知道如何将参数从主机应用程序传递给Web服务。

What is the best way to add this parameter to Web Service ? 将此参数添加到Web Service的最佳方法是什么?

Yes, you pass the connection string to the ServiceHost class without too much fuss. 是的,您不必过多地将连接字符串传递给ServiceHost类。 When your console program starts, get the connect string from your app.config file in the usual fashion, then store this value in a public static string variable in the public class of your console program. 当控制台程序启动时,以通常的方式从app.config文件中获取连接字符串,然后将此值存储在控制台程序的public类中的public static string变量中。 Once you've done that, from within your ServiceHost class you can retrieve it from the console public class. 完成此操作后,您可以从ServiceHost类中从控制台公共类中检索它。

Something like: 就像是:

 //your console program 
 public class WebHostConsole
 {
   private static string sConnectString;
   sConnectString = System.Configuration.ConfigurationManager.AppSettings("dbconn");
   public static string ConnectionString 
   {
     get { return sConnectString; }
   } 
   //rest of the code

  ServiceHost host = new ServiceHost(typeof(MyService));
  host.Open();
 }

Then, in the constructor of your web service class, reference the ConnectString property of your WebHostConsole class, and you're web service will have a copy of the connect string. 然后,在Web服务类的构造函数中,引用WebHostConsole类的ConnectString属性,您将在Web服务中获得连接字符串的副本。 Something like: 就像是:

//your service host class
public class MyService : iMyService
{
   public MyService()
   {
      String _ConnectString = WebHostConsole.ConnectionString;
      //rest of your constructor

   }
}

Update: 更新:

The way this works in my code is that I don't reference the web service functional class, I reference it's interface class. 这在我的代码中的工作方式是我没有引用Web服务功能类,而是引用了接口类。

 ServiceHost host = new ServiceHost(typeof(iMyService));

As you probably know, build the WCF interface class with the opreration and data contract definitions, then implement that interface in your working code. 您可能知道,使用操作和数据协定定义构建WCF接口类,然后在您的工作代码中实现该接口。 when you create your ServiceHost, reference in typeOf the interface, not the working class. 创建ServiceHost时,请在typeOf接口中引用,而不是在工作类中引用。 That will eliminate the circular reference. 这将消除循环引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Web服务配置问题 - Web Service Configuration Problem 使用纯文本密码对Web服务进行正确的绑定配置 - Proper binding configuration for web service with plaintext password 客户端配置以使用WCF JSON Web服务 - Client configuration to consume WCF JSON web service Windows服务调用WCF服务(Web http配置) - Windows service calling WCF service (web http configuration) Service Fabric-在同一Web服务的多个实例之间同步配置 - Service Fabric - Sync configuration between multiple instances of the same web service Web服务元数据的配置通过https提供错误 - Configuration for web service metadata gives error over https 从 WCF web 服务中的配置文件读取 - Reading from configuration file within WCF web service 相同的Web服务项目,不同的配置,无法调试 - Same web service project, different configuration, can't debug .Net 6 Web API 使用 Azure 配置服务 2 服务 AuthN/AuthZ - .Net 6 Web API configuration for service-2-service AuthN/AuthZ using Azure azure 应用服务从 web.debug.config 读取自定义环境变量而不是配置菜单变量 - azure app service reading custom environment variables from web.debug.config not configuration menu variables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM