我在 Microsoft 文档的帮助下创建了一个简单的 WCF 服务。
我公开了两个端点
1.服务端点(/CalculationService)和
2.服务端点(/CalculationService)。服务元数据端点 (/mex)
该服务正确托管在控制台应用程序内,我可以从浏览器浏览服务baseAddress和baseAddress?wsdl。
这里的问题是-
当我尝试浏览端点 /mex 和 /CalculationService 时,出现 Bad request 400 错误。
这里到底出了什么问题。找到以下托管应用程序的代码。
static void Main(string[] args)
{
// Step 1 of the address configuration procedure: Create a URI to serve as the base address.
Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service/");
// Step 2 of the hosting procedure: Create ServiceHost
ServiceHost selfHost = new ServiceHost(typeof(Service1), baseAddress);
try
{
// Step 3 of the hosting procedure: Add a service endpoint.
// Step 4 of the hosting procedure: Enable metadata exchange.
ServiceMetadataBehavior smb = selfHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (smb == null)
smb = new ServiceMetadataBehavior();
//ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
selfHost.Description.Behaviors.Add(smb);
selfHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, new BasicHttpBinding(), "mex");
// Step 5 of the hosting procedure: Start (and then stop) the service.
selfHost.AddServiceEndpoint(typeof(IService1), new WSHttpBinding(), "CalculatorService");
selfHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
// Close the ServiceHostBase to shutdown the service.
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occurred: {0}", ce.Message);
selfHost.Abort();
}
}
我没有在 web.config 文件中明确添加任何内容。
这是我的 WCF 服务的 web.config 文件。
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<!--<serviceMetadata httpGetEnabled="false" httpsGetEnabled="false"/>-->
<!-- 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>
</behaviors>
<!--<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>-->
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
因为一项服务中可以有多个端点,所以指定它们很重要。
他们确实在工作。元数据交换端点用于交换元数据。如果客户端应用程序无法到达此端点,您将无法引用服务。
另一个端点是公开方法。如果您能够使用它并使用它的方法,那么它也可以工作。