使用Web服务获取对象列表

时间:2014-07-17 16:19:13

标签: c# wcf

无法添加服务。可能无法访问服务元数据。确保您的服务正在运行并公开元数据。

我经过长时间的努力搜寻,却发现没有任何帮助。哪里错了?我真的不知道该怎么办。我在下面写了所有细节。我尝试过但没有成功。

我的代码:

[ServiceContract]

    public interface IUsersService
    {
        [OperationContract]
        void DoWork();

        [OperationContract]
        List<User> GetUsers();
    }




 public class UsersService : IUsersService
    {
        public void DoWork()
        {

        }

        public List<User> GetUsers()
        {
            var users=new List<User>();
            var sqlConnection =
                new SqlConnection(conString);
            var sqlDataAdapter=new SqlDataAdapter("select * from users",sqlConnection);

            var dt = new DataTable();

            sqlDataAdapter.Fill(dt);

            foreach (DataRow dataRow in dt.Rows)
            {
                users.Add(new User(dataRow["Name"].ToString()));
            }

            return users;
        }
    }

    public class User
    {
        public string UserName { get; set; }

        public User(string name)
        {
            UserName = name;
        }
    }

网络配置:

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="JuventusNewsWebService.UsersServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="JuventusNewsWebService.UsersServiceBehavior"
          name="JuventusNewsWebService.UsersService">
        <endpoint address="" binding="basicHttpBinding" contract="JuventusNewsWebService.IUsersService">
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
  </system.serviceModel>

错误:

> Error: Cannot obtain Metadata from
> http://lo cal host:31842/UsersService.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:31842/UsersService.svc    Metadata
> contains a reference that cannot be resolved:
> 'http://localhost:31842/UsersService.svc'.    Content Type
> application/soap+xml; charset=utf-8 was not supported by service
> http://localhost:31842/UsersService.svc.  The client and service
> bindings may be mismatched.    The remote server returned an error:
> (415) Cannot process the message because the content type
> 'application/soap+xml; charset=utf-8' was not the expected type
> 'text/xml; charset=utf-8'...

2 个答案:

答案 0 :(得分:0)

可能是你在服务中缺少主持人:

 <services>
      <service name="WcfServiceLibrary1.UsersService">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/UsersService/" />
          </baseAddresses>
        </host>
....
  </service>
    </services>

答案 1 :(得分:0)

通常,如果您在IIS中托管服务,则不需要定义baseAddress。

虽然错误消息中的建议可能有效,但请注意该解决方案适用于旧版Web服务。对于WCF,您不需要该设置。

有关更多详细信息,请查看此CodeProject文章。 http://www.codeproject.com/Articles/627240/WCF-for-the-Real-World-Not-Hello-World

对于您的问题,请检查示例代码,特别是app.config处理相对网址并激活该服务。