1
我正嘗試以編程方式創建端點,並且我不想在配置文件中指定端點配置。如何僅從瀏覽器調用RESTful端點?
我想在添加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>
現在,當我嘗試調用從瀏覽器我的終點類似下面,我得到沒有迴應:
我試過,但遺憾地說,我仍然無法打電話給我的方法 –