【问题标题】:WCF service selfhostingWCF 服务自托管
【发布时间】:2012-04-07 12:17:39
【问题描述】:

我正在创建一个自托管的 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>

【问题讨论】:

  • 来自您的 app.config:&lt;!-- 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. --&gt;。看起来您已经创建了一个 WCF 服务库。如果是这种情况,您需要将 WCF 配置数据放在使用库的应用程序的 App.config 文件中,而不是放在库的 App.config 中。

标签: wcf


【解决方案1】:

您需要将[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">

【讨论】:

  • 错误更改为“WCF 服务主机找不到任何服务元数据。这可能导致客户端应用程序运行不正常。请检查是否启用了元数据。”
  • @AlokKumar,你能发布你的 wcf 配置文件吗?
  • @AlokKumar,我没有看到配置文件。无论如何,我怀疑您缺少以下内容:&lt;serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /&gt; 在您的服务行为配置中
  • 我无法添加 app.config 文件
  • @AlokKumar,新的错误信息是什么?不幸的是,在不知道您的配置是什么的情况下,很难提供任何进一步的建议。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-07
  • 1970-01-01
  • 2013-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多