【问题标题】:Service metadata may not be accessible In WCF在 WCF 中可能无法访问服务元数据
【发布时间】:2014-12-03 22:25:54
【问题描述】:

我正在使用 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 是我的班级名称。 现在我在这里实现了我的界面

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.

【问题讨论】:

  • 尝试为元数据添加额外的端点。
  • @jadavparesh06:我添加了这个,但仍然遇到同样的问题

标签: c# asp.net ajax wcf


【解决方案1】:

对于带有 Ajax 的 WCF,服务的 &lt;endpoint /&gt; 应使用 WebHttpBinding 和在 &lt;endpointBehaviors&gt; 标记下配置的 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>

[&lt;service&gt; 标签的 'behaviourConfiguration' 属性应该设置为 为服务配置的相应行为。在这里您已经配置了行为 对于名称为“ServiceBehaviour”的服务]

check here the article 正确使用各种内置 WCF 绑定。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    • 2014-07-30
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多