WCF Restful服务端点在Visual Studio中给出404

时间:2014-04-26 17:29:43

标签: c# wcf rest

我一直在努力为使用Visual Studio创建的简单Web服务识别正确的端点,但我一直收到404错误。我之前只用C#做过一个其他的Web服务,之前我遇到了同样的错误,并且认为我终于找到了语法。

的web.config:

 <services>
      <service name="LDAPws.LDAPService" behaviorConfiguration="LDAPServiceBehavior">
        <endpoint address="" binding="webHttpBinding" contract="LDAPws.LDAPServiceInterface" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>

webServiceContract.cs:

namespace LDAPws
{
[ServiceContract(Name = "LDAPService")]

public interface LDAPServiceInterface
{

    //GET operation
    [OperationContract]
    [FaultContract(typeof(GetResourcesFaultContract))]
    [WebGet(UriTemplate = "/echo", ResponseFormat = WebMessageFormat.Json)]
    string sayHello();

webServiceLib.cs

namespace LDAPws
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class LDAPService : LDAPServiceInterface
    {


        public string sayHello()
        {
            return "Hello";

        }

URL:

http://localhost:8585/ldapws/ldapservice/echo

我无法弄清楚我做错了什么。我的目标是Dot Net Framework 4.0并在Visual Studio中托管。我当时认为默认URI是namespace.webservicename / parameters,但这对我不起作用。在web.config中添加baseaddress和address似乎也没有帮助。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

我找到了问题的根源:

我错过了添加global.asax文件并将服务添加到项目中。即,

    protected void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.Add(new ServiceRoute("LDAPws", new WebServiceHostFactory(), typeof(LDAPService)));  
    }