如何从浏览器中仅调用RESTful端点?

时间:2017-04-20 12:25:54

标签: c# wcf wcf-rest

我正在尝试以编程方式创建端点,我不想在配置文件中指定端点配置。

我想添加RESTful端点,因此在添加RESTful端点后,我应该可以从浏览器调用这些RESTful端点。

添加端点后,我在我的方法上放了一个调试器,但我的方法没有被调用,我看不到任何输出。

我的代码没有出错。根据我的理解,当我以编程方式添加此配置时,我不需要在配置文件中定义此配置。

Wcf服务代码:

public interface ICalculator
    {
        [OperationContract]
        [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,          UriTemplate = "Add/{n1}")]
        string Add(string n1);
    }

public class CalculatorService : ICalculator
    {
        public string Add(string n1)
        {
            return n1;
        }
    }

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2"/>
    <httpRuntime targetFramework="4.5.2"/>
    <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/>
    </httpModules>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>   
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="ApplicationInsightsWebTracking"/>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"
        preCondition="managedHandler"/>
    </modules>
    <directoryBrowse enabled="true"/>
    <validation validateIntegratedModeConfiguration="false"/>
  </system.webServer>
</configuration>

控制台应用托管wcf服务并添加端点:

class Program
    {
        static void Main(string[] args)
        {
            WebServiceHost serviceHost = new WebServiceHost(typeof(CalculatorService), new Uri("http://localhost:56264/CalculatorService.svc"));
            WebHttpBinding webHttpBinding = new WebHttpBinding();
            webHttpBinding.MaxReceivedMessageSize = 65536 * 2;
            webHttpBinding.MaxBufferPoolSize = 2147483647L;
            webHttpBinding.MaxBufferSize = 2147483647;
            webHttpBinding.MaxReceivedMessageSize = 2147483647L;
            serviceHost.AddServiceEndpoint(typeof(ICalculator), webHttpBinding, "CalculatorService");
            ServiceMetadataBehavior smb = serviceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
            if (smb == null)
                smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            serviceHost.Description.Behaviors.Add(smb);
            serviceHost.Open();
            Console.WriteLine("Press <ENTER> to terminate the service host");
            Console.ReadLine();
            serviceHost.Close();
        }
    }

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
</configuration>

现在,当我尝试从下面的浏览器中调用我的终点时,我得不到任何响应:

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

WebServiceHost Constructor中的Uri应该只包含服务的基地址。

试试这段代码:

WebServiceHost serviceHost = new WebServiceHost(typeof(CalculatorService),
                                                new Uri("http://localhost:56264"));