【问题标题】:How to call only RESTful endpoint from browser?如何从浏览器仅调用 RESTful 端点?
【发布时间】:2017-09-17 02:05:04
【问题描述】:

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

我想添加 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>

现在,当我尝试从如下浏览器调用我的端点时,我没有得到任何响应:

【问题讨论】:

    标签: c# wcf wcf-rest


    【解决方案1】:

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

    试试这个代码:

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

    【讨论】:

    • 我试过了,但很抱歉我仍然无法调用我的方法
    猜你喜欢
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    • 2013-06-28
    相关资源
    最近更新 更多