【发布时间】:2012-07-11 02:01:15
【问题描述】:
我将 Wcf 服务库 项目添加到解决方案中,它创建了 2 个类(Service1、IService1)和配置文件。现在我想在同一个解决方案中将该服务添加到我的 Console 项目中。我单击“添加服务引用 -> 发现”,它会找到该服务。
当我创建 Windows 类库 项目并在那里创建与刚刚创建的 Wcf 服务库 项目相同的示例,然后尝试将其添加为对我的 的引用>Console 项目,因此点击 Discover 不会返回任何内容。为什么?
当我创建 Wcf 服务库 项目或 Windows 类库 项目并在那里创建与 Wcf 服务库 相同的项目时有什么区别?
已编辑
Discover 仅在服务位于 Wcf 服务库 中时才有效。但是,一旦我转到其他项目(Console、Class Library),Discover 就再也找不到它了。为什么?
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="WcfServiceLibrary1.Service1">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address ="" binding="wsHttpBinding" contract="WcfServiceLibrary1.IService1">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!-- Metadata Endpoints -->
<!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
<!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</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>
Service1.cs
namespace WcfServiceLibrary1
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: Add your service operations here
}
// Use a data contract as illustrated in the sample below to add composite types to service operations
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
}
IService.cs
namespace WcfServiceLibrary1
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
}
【问题讨论】:
-
不确定您所说的发现是什么意思。服务库是一个添加了一些管道的类库。知道我从类库开始构建了一个 WCF 服务,它可以工作。三个项目。服务(类)、服务主机(控制台)和服务客户端(控制台)。客户端需要主机运行。您确定服务正在运行吗?
-
服务正在运行。我通过从 bin/Debug 目录运行服务,然后尝试添加服务引用来运行服务。
-
还是坏了?请贴出启动主机的代码。
-
这会对你有所帮助-tilr.blogspot.com/2014/06/…