C# 无法使用浏览器调用WCF方法
我开发了一个WCF服务应用程序,并将其部署到IIS8 使用浏览器,当我转到时,它会显示 “您已创建服务”和其他信息。这意味着服务已成功部署 但当我转到时,它显示HTTP 404未找到 这是服务合同:C# 无法使用浏览器调用WCF方法,c#,web-services,wcf,wcf-binding,C#,Web Services,Wcf,Wcf Binding,我开发了一个WCF服务应用程序,并将其部署到IIS8 使用浏览器,当我转到时,它会显示 “您已创建服务”和其他信息。这意味着服务已成功部署 但当我转到时,它显示HTTP 404未找到 这是服务合同: using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel;
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);
}
}
下面是实现的类:
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");
}
}
}
以下是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>
.尝试使用VisualStudio中的WCF测试客户端。它位于此处:C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\WcfTestClient.exe。打开测试客户端,添加服务并拨打电话
您可以使用fiddler或其他网络请求捕获工具查看您的服务请求的URL。希望这能让您进一步排除故障
以下是WCF测试客户端的MSDN。
它对我来说毫无问题
使用此URL:
和此配置文件:
<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>
转到IIS管理器
右键单击.SVC文件,选择浏览并确保配置文件中有正确的基址。您的基本地址看起来更像是IIS Express的地址。我的开发笔记本电脑上没有IIS。我将它部署到一个带有IIS的服务器上,该服务器上没有Visual Studio。您可以使用IIS express。这是VisualStudio附带的。当您的项目“设置为启动项目”时,没有启动或运行选项?我可以从我的开发笔记本电脑上运行“启动调试”。浏览器将以打开,其中显示我的开发文件目录。那接下来呢?当我转到时,它还向我显示HTTP 404。将localhost:57371/whateverthepathistoyourservice.svc放入wcf客户端我运行了WCFTestClient,然后在文件->添加服务中,我输入了我的服务。那接下来呢?在程序左窗格中,它只是显示“我的服务项目”,在右窗格中是“起始页”。在我的情况下,我的URL是什么?您正在使用“WcfService1”,这是什么?您的示例是否可以使用与我相同的名称空间和类名?请查看我的最后一条评论。当我浏览.SVC文件时,它会像在浏览器上一样打开。即使我在Visual Studio上运行“开始调试”,它始终是“localhost”。好的,请注意,此基址与您在问题中发布的基址不匹配。它是/testservice
还是/CustomerService.svc
?