VS 2010中的WCF端点地址

时间:2011-12-05 11:50:13

标签: c# .net wcf wcf-binding wcf-client

我有一个简单的网络服务。这是在使用VS 2010的网站中使用的。我在VS 2010中使用“添加服务引用”选项添加了服务引用。它工作正常。它将服务地址打印为http://localhost:3187/Service1.svc/MyFolder。但是当我在浏览器中键入此服务地址时,它会显示HTTP Error 400。

注意:当我在服务端点用address =“”替换address =“MyFolder”时,http://localhost:3187/Service1.svc会显示结果。

我应该在浏览器中输入正确的地址,以便在地址中使用“MyFolder”获取服务?

页面:

namespace ClientWebApp
{
  public partial class Default : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
        Service1Client myClientService = new Service1Client();
        Response.Write(myClientService.Endpoint.Address);

        string result = myClientService.GetData(7);
        lblName.Text = result;
    }
  }
}

合同:

namespace MyWCFServiceApplication
{
  [ServiceContract]
  public interface IService1
  {
    [OperationContract]
    string GetData(int value);
  }

  public class MyService : IService1
  {
    public string GetData(int value)
    {
        return string.Format("Now entered:  {0}", value);
    }
  }
}

配置:

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>

  <system.serviceModel>

    <services>
      <service name="MyWCFServiceApplication.MyService"
               behaviorConfiguration="WeatherServiceBehavior">

        <endpoint address="MyFolder"
                  binding="wsHttpBinding"
                  contract="MyWCFServiceApplication.IService1" />

        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="WeatherServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

  </system.serviceModel>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

2 个答案:

答案 0 :(得分:1)

请查看此问题的答案:WCF Endpoints & Binding Configuration Issues

引用:

  

在IIS中托管WCF服务时,使用以下格式形成服务的基本地址:   {协议}:// {主机}:{端口} / {的applicationName} / {svcFileName}。这是   您可以浏览的地址以获取WCF帮助页面和/或   元数据(在默认配置上)。

     

形成端点的实际地址(客户端需要的地址)   使用),使用以下格式:   {serviceBaseAddress} / {的EndpointAddress}

在您的情况下,{endpointAddress}MyFolder,这说明了您可以使用http://localhost:3187/Service1.svc/MyFolder地址添加服务引用的原因。但是,这不是您的帮助页面和元数据信息呈现的地址,因此您在http://.../*.svc/MyFolder上获得HTTP错误400这一事实并不令人意外。

答案 1 :(得分:0)

尝试在您尝试使用的网址末尾添加“?wsdl”。

相关问题