WCF服务,可能无法访问服务元数据

时间:2016-04-05 20:59:43

标签: c# web-services wcf web-config wcf-rest

在visual studio 2015 c#上运行F5之后,它会出错: 无法添加服务。可能无法访问服务元数据。确保您的服务正在运行并公开元数据。

  

错误:无法从http://localhost:56062/RestServiceImpl.svc/auth获取元数据如果这是您有权访问的Windows(R)Communication Foundation服务,请检查您是否已在指定地址启用元数据发布。有关启用元数据发布的帮助,请参阅http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange错误URI上的MSDN文档:http://localhost:56062/RestServiceImpl.svc/auth元数据包含无法解析的引用:' http://localhost:56062/RestServiceImpl.svc/auth'。远程服务器返回意外响应:(400)错误请求。远程服务器返回错误:(400)Bad Request.HTTP GET错误URI:http://localhost:56062/RestServiceImpl.svc/auth下载' http://localhost:56062/RestServiceImpl.svc/auth'时出错。请求失败,HTTP状态为405:Method Not Allowed。

我知道有很多问题在询问这个问题,但仍然无法找到解决方案。

  • 的web.config

`          

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="RestServiceTest.RestServiceImpl" behaviorConfiguration="ServiceBehaviour">
        <!-- Service Endpoints-->
        <!--Unless fully qualified, address is relative to base address supplied above-->
        <endpoint address="" binding="webHttpBinding" contract="RestServiceTest.IRestServiceImpl" behaviorConfiguration="web"/>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <!-- To avoid disclosing metadata information, set the value below to false 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="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment 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>

`

  • IRestServiceImpl.cs

`     使用系统;     使用System.Collections.Generic;     使用System.Linq;     使用System.Runtime.Serialization;     使用System.ServiceModel;     使用System.ServiceModel.Web;     使用System.Text;

namespace RestServiceTest
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IRestServiceImpl" in both code and config file together.
    [ServiceContract]
    public interface IRestServiceImpl
    {
        [OperationContract]
        [WebInvoke(Method="POST",
            ResponseFormat =WebMessageFormat.Xml,
            RequestFormat =WebMessageFormat.Xml,
            BodyStyle =WebMessageBodyStyle.Bare,
            UriTemplate ="auth")]
        ResponseData Auth(RequestData rData);
    }
    [DataContract(Namespace = "http://www.eysnap.com/mPlayer")]
    public class RequestData
    {
        [DataMember]
        public string details { get; set; }
    }
    [DataContract]
    public class ResponseData
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Age { get; set; }
        [DataMember]
        public string Exp { get; set; }
        [DataMember]
        public string Technology { get; set; }
    }

    }

`

  • RestServiceImpl.svc

`

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace RestServiceTest
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "RestServiceImpl" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select RestServiceImpl.svc or RestServiceImpl.svc.cs at the Solution Explorer and start debugging.
    public class RestServiceImpl : IRestServiceImpl
    {
        public ResponseData Auth(RequestData rData)
        {
            //Call Bill here
            var data = rData.details.Split('|');
            var response = new ResponseData
            {
                Name = data[0],
                Age = data[1],
                Exp = data[2],
                Technology = data[3]
            };
            return response;
        }
    }
}

`

0 个答案:

没有答案