2017-08-21 63 views
0

我目前正在使用wcf服务,而服务正在运行localhost.I在Wcf服务中有一些方法。当我想通过键入例如http://localhost:50028/StudentService.svc/GetAllStudent/ 其显示以下错误来访问本地主机的方法时,我正面临一些错误。WCF服务方法不起作用

**

Request Error 
The server encountered an error processing the request. Please see the service help page for constructing valid requests to the service. 

** 这里是我的代码的形式WCF服务....

[ServiceContract] 
    public interface IStudentService 
    { 

     [OperationContract] 
     [WebInvoke(Method = "GET", 
      RequestFormat = WebMessageFormat.Json, 
      ResponseFormat = WebMessageFormat.Json, 
      UriTemplate = "/GetAllStudent/")] 
     List<StudentDataContract> GetAllStudent(); 

     [OperationContract] 
     [WebGet(RequestFormat = WebMessageFormat.Json, 
      ResponseFormat = WebMessageFormat.Json, 
      UriTemplate = "/GetStudentDetails/{StudentId}")] 
     StudentDataContract GetStudentDetails(string StudentId); 

     [OperationContract] 
     [WebInvoke(Method = "POST", 
      RequestFormat = WebMessageFormat.Json, 
      ResponseFormat = WebMessageFormat.Json, 
      UriTemplate = "/AddNewStudent")] 
     bool AddNewStudent(StudentDataContract student); 

     [OperationContract] 
     [WebInvoke(Method = "PUT", 
      RequestFormat = WebMessageFormat.Json, 
      ResponseFormat = WebMessageFormat.Json, 
      UriTemplate = "/UpdateStudent")] 
     void UpdateStudent(StudentDataContract contact); 

     [OperationContract] 
     [WebInvoke(Method = "DELETE", 
      RequestFormat = WebMessageFormat.Json, 
      ResponseFormat = WebMessageFormat.Json, 
      UriTemplate = "DeleteStudent/{StudentId}")] 
     void DeleteStudent(string StudentId); 

    } 

} 

这是我实现的代码...

public class StudentService : IStudentService 
    { 
     StudentManagementEntities ctx; 

     public StudentService() 
     { 
      ctx = new StudentManagementEntities(); 
     } 

     public List<StudentDataContract> GetAllStudent() 
     { 
      //if (HttpContext.Current.Request.HttpMethod == "GetAllStudent") 
      // return null; 

      var query = (from a in ctx.Students 
         select a).Distinct(); 

      List<StudentDataContract> studentList = new List<StudentDataContract>(); 

      query.ToList().ForEach(rec => 
      { 
       studentList.Add(new StudentDataContract 
       { 
        StudentID = Convert.ToString(rec.StudentID), 
        Name = rec.Name, 
        Email = rec.Email, 
        EnrollYear = rec.EnrollYear, 
        Class = rec.Class, 
        City = rec.City, 
        Country = rec.Country 
       }); 
      }); 
      return studentList; 
     } 

     public StudentDataContract GetStudentDetails(string StudentId) 
     { 
      StudentDataContract student = new StudentDataContract(); 

      try 
      { 
       int Emp_ID = Convert.ToInt32(StudentId); 
       var query = (from a in ctx.Students 
          where a.StudentID.Equals(Emp_ID) 
          select a).Distinct().FirstOrDefault(); 

       student.StudentID = Convert.ToString(query.StudentID); 
       student.Name = query.Name; 
       student.Email = query.Email; 
       student.EnrollYear = query.EnrollYear; 
       student.Class = query.Class; 
       student.City = query.City; 
       student.Country = query.Country; 
      } 
      catch (Exception ex) 
      { 
       throw new FaultException<string> 
         (ex.Message); 
      } 
      return student; 
     } 

     public bool AddNewStudent(StudentDataContract student) 
     { 
      try 
      { 
       Student std = ctx.Students.Create(); 
       std.Name = student.Name; 
       std.Email = student.Email; 
       std.Class = student.Class; 
       std.EnrollYear = student.EnrollYear; 
       std.City = student.City; 
       std.Country = student.Country; 

       ctx.Students.Add(std); 
       ctx.SaveChanges(); 
      } 
      catch (Exception ex) 
      { 
       throw new FaultException<string> 
         (ex.Message); 
      } 
      return true; 
     } 

     public void UpdateStudent(StudentDataContract student) 
     { 
      try 
      { 
       int Stud_Id = Convert.ToInt32(student.StudentID); 
       Student std = ctx.Students.Where(rec => rec.StudentID == Stud_Id).FirstOrDefault(); 
       std.Name = student.Name; 
       std.Email = student.Email; 
       std.Class = student.Class; 
       std.EnrollYear = student.EnrollYear; 
       std.City = student.City; 
       std.Country = student.Country; 

       ctx.SaveChanges(); 
      } 
      catch (Exception ex) 
      { 
       throw new FaultException<string> 
         (ex.Message); 
      } 
     } 

     public void DeleteStudent(string StudentId) 
     { 
      try 
      { 
       int Stud_Id = Convert.ToInt32(StudentId); 
       Student std = ctx.Students.Where(rec => rec.StudentID == Stud_Id).FirstOrDefault(); 
       ctx.Students.Remove(std); 
       ctx.SaveChanges(); 
      } 
      catch (Exception ex) 
      { 
       throw new FaultException<string> 
         (ex.Message); 
      } 
     } 
    } 
} 

这里是web.config文件..

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </configSections> 
    <appSettings> 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.5"> 
     <assemblies> 
     <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> 
     </assemblies> 
    </compilation> 
    <httpRuntime targetFramework="4.5" /> 
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, set the values below to false before deployment --> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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> 
      <webHttp helpEnabled="True"/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <protocolMapping> 
     <add binding="webHttpBinding" scheme="http" /> 
    </protocolMapping> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" 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> 
    <connectionStrings> 
    <add name="StudentManagementEntities" connectionString="metadata=res://*/SchoolManagement.csdl|res://*/SchoolManagement.ssdl|res://*/SchoolManagement.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=KHUNDOKARNIRJOR\KHUNDOKERNIRJOR;initial catalog=Student;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> 
    </connectionStrings> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 
</configuration> 

我无法从本地主机它总是显示此错误

请求错误 服务器遇到错误处理请求访问。请参阅服务帮助页面以构建对服务的有效请求。

这里是截屏Click here to see the out put

请任何帮助,将不胜感激..

回答

0

我没有看到你的配置文件“服务”元素。请看下面的链接来配置你的服务。 Configuring Services

另一种方法是使用visual studio创建一个wcf服务。 Visual Studio会为你生成适当的配置文件。然后你可以相应地替换方法,接口和配置。

我看到你正在尝试返回一个列表。我不认为你可以通过List作为返回参数。

+0

我更新了我的问题 – Rasel

0

请检查 “ http://localhost:50028/StudentService.svc” 另一项建议,请 从uriTemplate删除 “/” 的所有方法。只需写“GetAllStudent”而不是 “/ GetAllStudent /”。

+0

对不起,我改变了它,但它的响应方式与 – Rasel

+0

Rasel一样,请检查你的web配置文件。服务和行为标记丢失, –

0
> <services> 
>  <service name="" behaviorConfiguration="serviceBehavior"> 
>   <endpoint address="" binding="webHttpBinding" contract="" behaviorConfiguration="web"/> 
>  </service> 
>  </services> 
>  <behaviors> 
>  <serviceBehaviors> 
>   <behavior name="serviceBehavior"> 
>   <serviceMetadata httpGetEnabled="true"/> 
>   <serviceDebug includeExceptionDetailInFaults="false"/> 
>   </behavior> 
>  </serviceBehaviors> 
>  <endpointBehaviors> 
>   <behavior name="web"> 
>   <webHttp/> 
>   </behavior> 
>  </endpointBehaviors> 
>  </behaviors> 

当我看有服务和行为标签从webconfig失踪。