如何获取 WSDL/描述,以便我可以在另一个项目中添加 WebReference?

发布于 2024-11-30 02:58:37 字数 1850 浏览 0 评论 0原文

我知道如何在 Visual Studio 中添加 WebReference,非常简单。

我还知道如何创建普通的 ASP.NET Web 服务项目,但这不是我在这里所做的。

因此,我运行的 WebService 如下所示:

try
{
    if (host != null)
    {
         host.Close();
         host = null;
    }
    baseAddress = new Uri("http://example.com:8080");
    host = new WebServiceHost(typeof(MyProxy), baseAddress);

    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
    smb.HttpGetEnabled = true;
    host.Description.Behaviors.Add(smb);

    host.Opened += new EventHandler(host_Opened);
    host.Closed += new EventHandler(host_Closed);

    System.ServiceModel.Description.ServiceEndpoint se = host.AddServiceEndpoint(typeof(IMyProxy), new WebHttpBinding(), baseAddress);
    se.Behaviors.Add(new System.ServiceModel.Description.WebHttpBehavior());
    host.Open();
 }
 catch (Exception e)
 {
 }

 // .... stuff ....

[ServiceContract]
public interface IMyProxy
{
    [OperationContract]
    [WebGet(UriTemplate = "GetArea?searchString={searchString}")]
    GetAreaResult GetArea(string searchString);
}

// more stuff of course follows here

问题是,当我尝试在 Visual Studio 中向上述服务添加 WebReference 时,出现错误。

“添加服务引用”--> “添加网络参考” 并在 URL 中写下我的 URL, http://example.com:8080

然后我得到“Service . ..未找到端点。”以及“添加 Web 引用”框中的错误消息:

下载“http://example.com:8080/”时出错。请求 失败,HTTP 状态为 404:未找到。下载时出现错误 'http://example.com:8080/$metadata'。 HTTP 请求失败 状态 404:未找到。

如果我打开网络浏览器并直接转到 http://example.com:8080/GetArea该服务按预期调用/执行。

因此,为了更简短地重新表述该问题:WSDL/描述不存在,因此我无法添加 Web 服务引用。

I know how to add a WebReference in Visual Studio, easy enough.

I also know how to create a normal ASP.NET Web Service project, but that's not what I am doing here.

So, the WebService I have running looks like this:

try
{
    if (host != null)
    {
         host.Close();
         host = null;
    }
    baseAddress = new Uri("http://example.com:8080");
    host = new WebServiceHost(typeof(MyProxy), baseAddress);

    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
    smb.HttpGetEnabled = true;
    host.Description.Behaviors.Add(smb);

    host.Opened += new EventHandler(host_Opened);
    host.Closed += new EventHandler(host_Closed);

    System.ServiceModel.Description.ServiceEndpoint se = host.AddServiceEndpoint(typeof(IMyProxy), new WebHttpBinding(), baseAddress);
    se.Behaviors.Add(new System.ServiceModel.Description.WebHttpBehavior());
    host.Open();
 }
 catch (Exception e)
 {
 }

 // .... stuff ....

[ServiceContract]
public interface IMyProxy
{
    [OperationContract]
    [WebGet(UriTemplate = "GetArea?searchString={searchString}")]
    GetAreaResult GetArea(string searchString);
}

// more stuff of course follows here

The problem is that when I try to add a WebReference to the above service in Visual Studio, I get an error.

"Add Service Reference" --> "Add Web Reference"
and in the URL I write my URL, http://example.com:8080

Then I get "Service ... Endpoint not found." and the error message in the Add Web Reference box:

There was an error downloading 'http://example.com:8080/'. The request
failed with HTTP status 404: Not Found. There was an error downloading
'http://example.com:8080/$metadata'. The request failed with HTTP
status 404: Not Found.

If I open up a web browser and go directly to http://example.com:8080/GetArea the service is called/executed as expected.

So to rephrase the problem shorter: The WSDL/description isn't there, so I cannot add a Web Service reference.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妖妓 2024-12-07 02:58:37

这里的问题是,WebServiceHost 将删除您在添加 ServiceMetadataBehavior 时尝试实现的功能。查看 WebServiceHost 处的 dotPeek(反射器),在 OnOpening 方法内有:

ServiceDebugBehavior serviceDebugBehavior = this.Description.Behaviors.Find<ServiceDebugBehavior>();
  if (serviceDebugBehavior != null)
  {
    serviceDebugBehavior.HttpHelpPageEnabled = false;
    serviceDebugBehavior.HttpsHelpPageEnabled = false;
  }
  ServiceMetadataBehavior metadataBehavior = this.Description.Behaviors.Find<ServiceMetadataBehavior>();
  if (metadataBehavior != null)
  {
    metadataBehavior.HttpGetEnabled = false;
    metadataBehavior.HttpsGetEnabled = false;
  }

WebServiceHost 设计用于与 REST/JSON 服务一起使用通常没有定义的合同,因此元数据(mex)被禁用。

如果您尝试创建基于 SOAP 的服务,则需要使用标准 ServiceHost。看起来这就是您想要的,因为您正在尝试通过 VS 添加服务引用。

如果您尝试创建 REST/JSON 服务,可以使用 WebServiceHost

The problem here is that the WebServiceHost will remove the functionality that you are trying to achieve when adding the ServiceMetadataBehavior. Looking in dotPeek (reflector) at the WebServiceHost, inside the OnOpening method there is:

ServiceDebugBehavior serviceDebugBehavior = this.Description.Behaviors.Find<ServiceDebugBehavior>();
  if (serviceDebugBehavior != null)
  {
    serviceDebugBehavior.HttpHelpPageEnabled = false;
    serviceDebugBehavior.HttpsHelpPageEnabled = false;
  }
  ServiceMetadataBehavior metadataBehavior = this.Description.Behaviors.Find<ServiceMetadataBehavior>();
  if (metadataBehavior != null)
  {
    metadataBehavior.HttpGetEnabled = false;
    metadataBehavior.HttpsGetEnabled = false;
  }

The WebServiceHost is designed to be used with REST/JSON services which typically have no defined contract, hence why metadata (mex) is disabled.

If you are trying to create a SOAP based service, you need to use a standard ServiceHost. It looks like this is what you want, as you are trying to add the service reference through VS.

If you are trying to create a REST/JSON service, you can use WebServiceHost.

浅听莫相离 2024-12-07 02:58:37

您需要将 ServiceMetadataBehavior 添加到主机,请参阅 MSDN 参考< /a>.

// Enable Mex
host.Description.Behaviors.Add(new ServiceMetadataBehavior{ HttpGetEnabled = true });

You will need a ServiceMetadataBehavior added to the host refer MSDN Reference.

// Enable Mex
host.Description.Behaviors.Add(new ServiceMetadataBehavior{ HttpGetEnabled = true });
红颜悴 2024-12-07 02:58:37

我认为你需要为你的 web 服务创建 WSDL 或 disco 文件,为此在本地运行你的 web 服务,正如你所说,它执行得很好。在我的例子中,当我执行我的 web 服务时,我在页面顶部得到链接作为“服务描述” " 。当您单击此处时,您将在浏览器中获得 WSDL 文件。另一种方法是在查询字符串末尾添加“?wsdl”,您将获得 wsdl 文件。

I think you need to create WSDL or disco file for your webservice,For that run your webservice locally ,as you said it is executed fine.In my case when I execute my webservice I get link on the top of my page as "Service Description" .When you will click here you will get your WSDL file in browser.Another way is to add "?wsdl" at the end of your querystring ,You will get your wsdl file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文