繁体   English   中英

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

[英]Service metadata may not be accessible In WCF

我正在使用Ajax调用wcf服务,为此,我已经配置了web.config文件。 但是现在当我运行我的服务时,它给出了这样的错误。

Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata.

这是我的Interface

namespace WcfWithJson
{
    [ServiceContract]
    public interface IMyservice
    {
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
        UserDetails[] GetUserDetails(string Username);
    }
}

注意: userDetails是我的班级名称。 现在我已经在这里实现了mY接口

namespace WcfWithJson
{
   [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MyService : IMyservice
    {
        public UserDetails[] GetUserDetails(string Username)
        {
            string strConnection = ConfigurationManager.ConnectionStrings["MyDbEntities"].ConnectionString;
            List<UserDetails> userdetails = new List<UserDetails>();
            using (SqlConnection con = new SqlConnection(strConnection))
            {
              // some sql Code here
            }
            return userdetails.ToArray();
        }
    }
}

这是我的web.config文件

    <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <services>
      <service name="WcfWithJson.MyService" behaviorConfiguration="metadataBehavior">
        <endpoint address="" binding="basicHttpBinding" contract="WcfWithJson.IMyservice" behaviorConfiguration="EndPointBehavior" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="EndPointBehavior">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

这是完全错误:

Error: Cannot obtain Metadata from http://localhost:61762/MyService.svc If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address.
For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: http://localhost:61762/MyService.svc Metadata contains a reference that cannot be resolved: 'http://localhost:61762/MyService.svc'.
Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost:61762/MyService.svc. The client and service bindings may be mismatched. The remote server returned an error: (415) Unsupported Media Type.HTTP GET Error URI: http://localhost:61762/MyService.svc The HTML document does not contain Web service discovery information.

对于带有Ajax的WCF,服务的<endpoint />应该使用WebHttpBinding和在<endpointBehaviors>标记下配置的ASP.NET AJAX行为。

因此,条目应为:

<services>
 <service name="WcfWithJson.MyService" behaviorConfiguration="ServiceBehavior">
  <endpoint address="" binding="webHttpBinding" 
    contract="WcfWithJson.IMyservice" behaviorConfiguration="EndPointBehavior" />
  <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
 </service>
</services>

[ <service>标记的'behaviourConfiguration'属性应设置为为该服务配置的相应行为。 在这里,您已为名称为“ ServiceBehaviour”的服务配置了行为。

check here the article有关正确使用各种内置WCF绑定的文章。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM