2014-10-09 57 views
1

我打電話給我使用Ajax的wcf服務,爲此我配置了我的web.config文件。但現在當我運行我的服務時,它會發出這樣的錯誤。服務元數據可能無法訪問在WCF

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.

+0

嘗試爲元數據添加額外的端點。 jadavparesh06 2014-10-09 05:19:47

+0

@ jadavparesh06:我加了這個,但是仍然有同樣的問題 – 2014-10-09 05:33:19

回答

1

在WCF的情況下使用Ajax,該<endpoint />爲服務應該使用WebHttpBinding和ASP.NET AJAX行爲conf在<endpointBehaviors>標記下配置。

所以,條目應該是:

<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綁定。

相關問題