简体   繁体   English

无法使用浏览器调用 WCF 方法

[英]Unable to call WCF method with browser

I developed a WCF service application and deployed it to IIS 8.我开发了一个 WCF 服务应用程序并将其部署到 IIS 8。

With a browser, when I go to http://localhost:6000/CustomService.svc , it shows "You have created a service" and other information.使用浏览器,当我转到http://localhost:6000/CustomService.svc 时,它显示“您已创建服务”等信息。 This means the service is successfully deployed.这意味着该服务已成功部署。

But when I go to http://localhost:6000/testservice/date/2016/12/1 , it showed HTTP 404 Not Found.但是当我访问http://localhost:6000/testservice/date/2016/12/1 时,它显示 HTTP 404 Not Found。

Here is service contract:以下是服务合同:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    using System.ServiceModel.Web;

    namespace WCF
    {
        [ServiceContract]
        public interface ICustomService
        {
            [WebGet(UriTemplate = "date/{year}/{month}/{day}", ResponseFormat = WebMessageFormat.Xml)]
            [OperationContract]
            string GetDate(string day, string month, string year); 
        }
    }

Here is implemented class:这是实现的类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WCF
{
    public class CustomService : ICustomService
    {
        public string GetDate(string day, string month, string year)
        {
            return new DateTime(Convert.ToInt32(year), Convert.ToInt32(month), Convert.ToInt32(day)).ToString("dddd, MMMM dd, yyyy");
        }
    }
}

Here is Web.config:这是 Web.config:

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

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="CustomServiceBehavior" name="WCF.CustomService">
        <endpoint address="" behaviorConfiguration="webBehavior" binding="webHttpBinding" contract="WCF.ICustomService" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:6000/testservice" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="CustomServiceBehavior">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

Where could the problem be?问题可能出在哪里? I basically copied most of the stuff from https://weblogs.asp.net/kiyoshi/wcf-using-webhttpbinding-for-rest-services .我基本上从https://weblogs.asp.net/kiyoshi/wcf-using-webhttpbinding-for-rest-services复制了大部分内容。

It worked with no problem for me.它对我来说没有问题。

在此处输入图片说明

using this URL:使用这个网址:

http://localhost/WcfService1/Service1.svc/date/2017/1/31 http://localhost/WcfService1/Service1.svc/date/2017/1/31

and this config file:和这个配置文件:

 <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="NewBinding0">
          <security>
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="WcfService1.Service1">
        <endpoint address="" behaviorConfiguration="NewBehavior0" binding="webHttpBinding"
          bindingConfiguration="" contract="WcfService1.IService1" />
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="NewBehavior0">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

Go to IIS Manager right-click your .SVC file, select browse and make sure you have the correct base address in your config file.转到IIS Manager右键单击您的 .SVC 文件,选择浏览并确保您的配置文件中有正确的基址。 Your base address looks more like an IIS Express address.您的基地址看起来更像是IIS Express地址。

Try using the WCF test client that comes baked into Visual Studio.尝试使用融入 Visual Studio 的 WCF 测试客户端。 It's located here: C:\\Program Files\\Microsoft Visual Studio 9.0\\Common7\\IDE\\WcfTestClient.exe.它位于:C:\\Program Files\\Microsoft Visual Studio 9.0\\Common7\\IDE\\WcfTestClient.exe。 Open the test client, add the service and make your call.打开测试客户端,添加服务并进行调用。

You can use fiddler or some other network request capturing tool to see what URL is being requested by your service.您可以使用 fiddler 或其他一些网络请求捕获工具来查看您的服务请求的 URL。 Hopefully that should allow you to troubleshoot further.希望这能让您进一步排除故障。

Here is the MSDN for the WCF test client.这是 WCF 测试客户端的 MSDN。 https://msdn.microsoft.com/en-us/library/bb552364(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/bb552364(v=vs.110).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM