2012-04-07 231 views
0

我正在使用自託管創建WCF服務。我發現了以下錯誤,即:WCF服務自託管

目標程序集不包含服務類型。您可能需要調整此程序集的代碼訪問安全性策略。

的代碼如下:

namespace MyJobs 
{ 
    public interface IJobsSvc 
    { 
     [OperationContract] 
     DataSet GetJobs(); 

     [OperationContract] 
     Job GetJobInfo(int JobId); 

     [OperationContract] 
     List<Job> GetAllJobs(); 
    } 
} 

namespace MyJobs 
{ 
    [DataContract] 
    public class Job 
    { 
     [DataMember] 
     public int JobId { get; set;} 

     [DataMember] 
     public string Description{get;set;} 

     [DataMember] 
     public int MinLevel { get; set; } 

     [DataMember] 
     public int MaxLevel { get; set; } 
    } 
} 

namespace MyJobs 
{ 
    public class JobsSvc:IJobsSvc 
    { 
     #region IJobsSvc Members 

     public System.Data.DataSet GetJobs() 
     { 
      string str = @"data source=PERSONAL-659BE4;database=practice;integrated security=true"; 
      DataSet ds = new DataSet(); 
      SqlConnection cn = new SqlConnection(str); 
      SqlDataAdapter da = new SqlDataAdapter("select * from Job1",cn); 
      da.Fill(ds); 
      return ds; 

     } 

     public Job GetJobInfo(int JobId) 
     { 
      string str = @"data source=PERSONAL-659BE4;database=practice;integrated security=true"; 
      SqlConnection cn = new SqlConnection(str); 
      SqlCommand cmd = new SqlCommand("select * from Job1 where JobId="+JobId,cn); 
      cn.Open(); 
      SqlDataReader dr = cmd.ExecuteReader(); 
      Job obj = new Job(); 
      if (dr.Read()) 
      { 
       obj.JobId = JobId; 
       obj.Description = dr[1].ToString(); 
       obj.MinLevel = Convert.ToInt32(dr[2]); 
       obj.MaxLevel = Convert.ToInt32(dr[3]); 
      } 
      else 
      { 
       obj.JobId = -1; 
      } 
      return obj; 
     } 

     public List<Job> GetAllJobs() 
     { 
      throw new NotImplementedException(); 
     } 

     #endregion 
    } 
} 

app.config文件是:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 

    <system.web> 
    <compilation debug="true" /> 
    </system.web> 
    <!-- When deploying the service library project, the content of the config file must be added to the host's 
    app.config file. System.Configuration does not support config files for libraries. --> 
    <system.serviceModel> 
    <services> 
     <service name="MyJobs.Job"> 
     <endpoint address="" binding="wsHttpBinding" contract="MyJobs.IJobsSvc"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8732/Design_Time_Addresses/Jobs/MyJobs/" /> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, 
      set the value below to false and remove the metadata endpoint above 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> 
    </behaviors> 
    </system.serviceModel> 

</configuration> 
+0

從你的app.config:'< - 當部署服務庫項目,配置文件的內容必須被添加到主機的app.config文件。 System.Configuration不支持庫的配置文件。 - >'。看起來你已經創建了一個WCF服務庫。如果是這種情況,則需要將WCF配置數據放入使用庫的應用程序的App.config文件中,而不是放在庫的App.config中。 – Tim 2012-04-07 20:47:53

回答

2

您需要將屬性添加[ServiceContract]IJobSvc接口

更新

創建公開元數據的行爲。

<serviceBehaviors> 
    <behavior name="SimpleServiceBehavior"> 
     <!-- To avoid disclosing metadata information, 
     set the value below to false and remove the metadata endpoint above 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> 

那麼這種行爲配置您的服務:

<service name="MyJobs.Job" behaviorConfiguration="SimpleServiceBehavior"> 
    <endpoint address="" binding="wsHttpBinding" contract="MyJobs.IJobsSvc"> 
+0

錯誤更改爲「WCF服務主機找不到任何服務元數據,這可能會導致客戶端應用程序運行不正常,請檢查元數據是否可用。」 – 2012-04-07 12:34:55

+0

@AlokKumar,你可以發佈你的wcf配置文件嗎? – Tung 2012-04-07 12:47:30

+0

這是我的app.config文件 – 2012-04-07 12:50:08