如何在C#的Web服务中包含自己的wsdl [英] How do I include my own wsdl in my web service in C#
问题描述
我的Web服务(旧的asmx样式)必须实现一个.wsdl文件.照顾好了.当我发布Web服务时,您可以使用?wsdl参数对其进行调用以获取生成的wsdl.
I have a .wsdl file that my web service (old asmx style) must implement. That is taken care of. When I publish the web service you can call it with ?wsdl parameter to get a generated wsdl.
我如何包含我的.wsdl文件,以便它是返回的文件而不是生成的文件?
How do I include my .wsdl file so that is the one that is returned instead of the generated one?
是否可以在我的Web服务类中使用属性?
Is it possible to do with an attribute in my web service class?
推荐答案
为避免在Web服务中的两个不同URL(即* .asmx?wsdl URL和自定义URL)上具有两个不同WSDL的混淆应用程序中,您可以编写一个HttpModule来拦截对* .asmx?wsdl URL的请求,并返回您的自定义WSDL.
To avoid the confusion of having two different WSDLs available on two different URLs (i.e., the *.asmx?wsdl URL and a custom URL) in your web service application, you could write an HttpModule that intercepts the request to the *.asmx?wsdl URL and returns your custom WSDL instead.
这是一个示例,该示例经过改编并简化了一些我之前编写的代码,这些代码使自定义WSDL可在标准* .asmx?wsdl URL上使用.
Here's an example, adapted and simplified from some code I previously wrote that makes a custom WSDL available at the standard *.asmx?wsdl URL.
using System;
using System.IO;
using System.Web;
using System.Web.Services.Configuration;
namespace DemoWebService
{
public class CustomWsdlModule :
IHttpModule
{
public void
Init(HttpApplication application)
{
// hook up to BeginRequest event on application object
application.BeginRequest += new EventHandler(this.onApplicationBeginRequest);
}
public void
Dispose()
{
}
private void
onApplicationBeginRequest(object source, EventArgs ea)
{
HttpApplication application = (HttpApplication)source;
HttpRequest request = application.Request;
HttpResponse response = application.Response;
// check if request is for WSDL file
if ( request.Url.PathAndQuery.EndsWith(".asmx?wsdl", StringComparison.InvariantCultureIgnoreCase) )
{
// if Documentation protocol is not allowed, throw exception
if ( (WebServicesSection.Current.EnabledProtocols & WebServiceProtocols.Documentation) == 0 )
{
throw new System.InvalidOperationException("Request format is unrecognized.");
}
// get path to physical .asmx file
String asmxPath = request.MapPath(request.Url.AbsolutePath);
// build path to .wsdl file; should be same as .asmx file, but with .wsdl extension
String wsdlPath = Path.ChangeExtension(asmxPath, ".wsdl");
// check if WSDL file exists
if ( File.Exists(wsdlPath) )
{
// read WSDL file
using ( StreamReader reader = new StreamReader(wsdlPath) )
{
string wsdlFileContents = reader.ReadToEnd();
// write WSDL to response and end response without normal processing
response.ContentType = "text/xml";
response.Write(wsdlFileContents);
response.End();
}
}
}
}
}
}
此简化代码假定您的自定义WSDL与扩展名为.wsdl的.asmx文件位于同一文件夹中. HttpModule需要通过web.config文件挂接到您的Web服务应用程序中:
This simplified code assumes that your custom WSDL is in the same folder as your .asmx file with a .wsdl extension. The HttpModule needs to be hooked into your web service application via the web.config file:
<?xml version="1.0"?>
<configuration>
<!-- ... -->
<system.web>
<!-- ... -->
<httpModules>
<add
type="DemoWebService.CustomWsdlModule"
name="CustomWsdlModule"/>
<!-- ... -->
</httpModules>
<!-- ... -->
</system.web>
<!-- ... -->
</configuration>
这篇关于如何在C#的Web服务中包含自己的wsdl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!