我从Web服务构建文件Service.dll
以供使用。现在,我想自动更新文件Service.dll
,以便更改Web服务。目前,我的方法是保存WSDL,用于将Web服务描述为Service.dll
资源,然后将此资源中的WSDL与直接来自Web服务的WSDL进行比较,如果它们不同,我将更新文件Service.dll。
这是我的代码C#:
public ServiceInspector(Uri serviceLocation)
{
if (serviceLocation.Query == string.Empty)
{
UriBuilder uriB = new UriBuilder(serviceLocation);
uriB.Query = "WSDL";
serviceLocation = uriB.Uri;
}
_serviceLocation = serviceLocation;
WebRequest wsdlWebRequest = WebRequest.Create(serviceLocation);
Stream wsdlRequestStream = wsdlWebRequest.GetResponse().GetResponseStream();
//Get the ServiceDescription from the WSDL file
ServiceDescription sd = ServiceDescription.Read(wsdlRequestStream);
string wsdlPath = @"C:\Users\John\Downloads\Compressed\Core\TestApp\Ref\Service.wsdl";
sd.Write(wsdlPath);
string sdName = sd.Services[0].Name;
ServiceDescriptionImporter sdImport = new ServiceDescriptionImporter();
sdImport.AddServiceDescription(sd, String.Empty, String.Empty);
sdImport.ProtocolName = "Soap";
sdImport.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties;
CodeNamespace codeNameSpace = new CodeNamespace();
CodeCompileUnit codeCompileUnit = new CodeCompileUnit();
codeCompileUnit.Namespaces.Add(codeNameSpace);
ServiceDescriptionImportWarnings warnings = sdImport.Import(codeNameSpace, codeCompileUnit);
if (warnings == 0)
{
StringWriter stringWriter = new StringWriter(System.Globalization.CultureInfo.CurrentCulture);
Microsoft.CSharp.CSharpCodeProvider prov = new Microsoft.CSharp.CSharpCodeProvider();
prov.GenerateCodeFromNamespace(codeNameSpace, stringWriter, new CodeGeneratorOptions());
//Compile the assembly
string[] assemblyReferences = new string[2] { "System.Web.Services.dll", "System.Xml.dll" };
CompilerParameters param = new CompilerParameters(assemblyReferences);
param.GenerateExecutable = false;
param.GenerateInMemory = false;
param.TreatWarningsAsErrors = false;
param.WarningLevel = 4;
//Embedd wsdl into Resources
param.EmbeddedResources.Add(wsdlPath);
param.OutputAssembly = @"C:\Users\John\Downloads\Compressed\Core\TestApp\Ref\Service.dll";
CompilerResults results = new CompilerResults(new TempFileCollection());
results = prov.CompileAssemblyFromDom(param, codeCompileUnit);
Assembly assembly = results.CompiledAssembly;
}
}
方法检查wsdl已更改:
private static bool IsServiceChanged(Uri uri)
{
Assembly assembly = Assembly.LoadFile(@"C:\Users\John\Downloads\Compressed\Core\TestApp\Ref\Service.dll");
Stream stream = assembly.GetManifestResourceStream("Service.wsdl");
if (string.IsNullOrEmpty(uri.Query))
{
UriBuilder uriBuilder = new UriBuilder(uri);
uriBuilder.Query = "WSDL";
uri = uriBuilder.Uri;
}
System.Net.WebRequest wsdlWebRequest = System.Net.WebRequest.Create(uri);
using (Stream wsdlRequestStream = wsdlWebRequest.GetResponse().GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
using (StreamReader reader2 = new StreamReader(wsdlRequestStream))
{
string result = reader.ReadToEnd();
string result2 = reader2.ReadToEnd();
if (string.Compare(result, result2, StringComparison.Ordinal) != 0)
{
return true;
}
}
return false;
}
当前,我的方法IsServiceChanged(Uri uri)
始终返回True
,因为写入文件的文件wsdl已更改排序顺序命名空间。 E.g:
<wsdl:definitions xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"....
这是wsdl直接从Web服务获取,然后写入文件,它将成为:
<wsdl:definitions xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:s="http://www.w3.org/2001/XMLSchema"
请给我一个更新此Service.dll的最佳解决方案?谢谢你的帮助!