Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Web服务和配置_C#_.net_Web Services_Configuration_.net 2.0 - Fatal编程技术网

C# Web服务和配置

C# Web服务和配置,c#,.net,web-services,configuration,.net-2.0,C#,.net,Web Services,Configuration,.net 2.0,我使用.NET2.0框架创建了一个WebService,该类基于具有WebServiceAttribute的接口,并使用IIS和ASMX文件托管它。Web服务当前从一个XML文件加载其配置 我想创建此服务的多个实例,每个实例都加载自己的配置 通过处理ASMX文件,我可以用不同的名称创建webservice的克隆,该名称将基于完全相同的实现。但它也加载了完全相同的配置文件,这使得它变得毫无用处 因此,我的问题是:创建任意数量的基于一个类的Web服务的最佳方法是什么,这些Web服务位于一个IIS虚拟

我使用.NET2.0框架创建了一个WebService,该类基于具有WebServiceAttribute的接口,并使用IIS和ASMX文件托管它。Web服务当前从一个XML文件加载其配置

我想创建此服务的多个实例,每个实例都加载自己的配置

通过处理ASMX文件,我可以用不同的名称创建webservice的克隆,该名称将基于完全相同的实现。但它也加载了完全相同的配置文件,这使得它变得毫无用处

因此,我的问题是:创建任意数量的基于一个类的Web服务的最佳方法是什么,这些Web服务位于一个IIS虚拟目录中,每个目录都加载不同的配置文件

解决方案 在Pavel Chuchuva的回答的帮助下,我创建了以下代码来处理配置的加载:

public class WebConfigManager
{
    public static T Load<T>() where T: new()
    {
        string location = 
            HttpContext.Current.Request.PhysicalPath + ".config";

        if (HttpContext.Current.Cache[location] is T)
        {
            return (T)HttpContext.Current.Cache[location];
        }

        using (Stream s = 
            new FileStream(location, FileMode.Open, FileAccess.Read))
        {                
            return (T)(HttpContext.Current.Cache[location] = 
                new XmlSerializer(typeof(T)).Deserialize(s));                
        }
    }
}

// example of the usage of WebConfigManager
public class MyWebService : IMyWebService 
{
    Config config = WebConfigManager.Load<Config>();
...
公共类WebConfigManager
{
公共静态T Load(),其中T:new()
{
字符串位置=
HttpContext.Current.Request.PhysicalPath+“.config”;
if(HttpContext.Current.Cache[location]为T)
{
返回(T)HttpContext.Current.Cache[location];
}
使用(流s=
新文件流(位置,FileMode.Open,FileAccess.Read))
{                
返回(T)(HttpContext.Current.Cache[位置]=
新的XmlSerializer(typeof(T))。反序列化(s));
}
}
}
//WebConfigManager的使用示例
公共类MyWebService:IMyWebService
{
Config=WebConfigManager.Load();
...

我建议将asmx放在不同的文件夹中,并在每个文件夹中放置一个web.config,其中包含特定web服务实例的设置。这是一种简单快捷的方法

您可以使用Web Service Enhancements 3.0创建一个WSE路由器,将对ASMX的调用重定向到该路由器,并让路由器将调用转发到正确的Web Service实例并传递额外的配置。这是一种更复杂的方法,但它允许您使用一个选择正确配置基础的Web Service实例在路由器传递的参数上。 有关WSE3.0的更多信息,请参阅MSDN


希望这有帮助!

复制并粘贴.asmx文件以创建web服务的多个实例(例如Service1.asmx、Service2.asmx等)

基于Context.Request.FilePath值加载配置文件:

public string LoadConfig()
{
   string configPath = Server.MapPath(this.Context.Request.FilePath + ".xml");
   using (XmlReader reader = XmlReader.Create(configPath))
   {
      // Will read Service1.asmx.xml, Service2.asmx.xml and so on
   }
}

@Pavel:很好的例子,但是您的XmlReader创建应该在using块中。我建议您在有人复制代码之前编辑您的答案,而没有意识到它只是一个示例。@All:WSE已经过时。它几乎不受支持,在Visual Studio 2008或更高版本中不起作用。除非您没有选择,否则不要使用它。