C# 使用web服务获取对象列表
未能添加服务。服务元数据可能无法访问。确保您的服务正在运行并公开元数据 我找了很久,但还是没有找到任何有用的东西。我哪里错了?我真的不知道该怎么办。我把所有细节写在下面。我试过了,但没有成功 我的代码:C# 使用web服务获取对象列表,c#,wcf,C#,Wcf,未能添加服务。服务元数据可能无法访问。确保您的服务正在运行并公开元数据 我找了很久,但还是没有找到任何有用的东西。我哪里错了?我真的不知道该怎么办。我把所有细节写在下面。我试过了,但没有成功 我的代码: [ServiceContract] public interface IUsersService { [OperationContract] void DoWork(); [OperationContract]
[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;
}
}
可能是您缺少服务中的主机:
<services>
<service name="WcfServiceLibrary1.UsersService">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/UsersService/" />
</baseAddresses>
</host>
....
</service>
</services>
通常,如果您在IIS中托管服务,则不需要定义基址,因为IIS会处理此问题 尽管来自错误消息的建议可能有效,但请注意,该解决方案适用于遗留Web服务。对于WCF,您不需要该设置 有关更多详细信息,请参阅本CodeProject文章。 对于您的问题,请检查示例代码,特别是app.config处理相对url并激活服务
> 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'...
<services>
<service name="WcfServiceLibrary1.UsersService">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/UsersService/" />
</baseAddresses>
</host>
....
</service>
</services>