gpt4 book ai didi

c# - 为什么在 "Windows Class Library"项目中创建时未发现 wcf 服务?

转载 作者:行者123 更新时间:2023-11-30 20:05:25 25 4
gpt4 key购买 nike

我将 Wcf 服务库 项目添加到解决方案中,它创建了 2 个类(Service1IService1)和配置文件。现在我想将该服务添加到同一解决方案中的 Console 项目中。我单击“添加服务引用 -> 发现”,它找到了该服务。

当我创建 Windows 类库 项目并在那里创建与刚刚创建的 Wcf 服务库 项目相同的示例,然后尝试将其添加为对我的 的引用>Console 项目,因此点击 Discover 不会返回任何内容。为什么?

创建Wcf Service Library 项目或Windows Class Library 项目和在Wcf Service Library 中创建时有什么区别?

已编辑

仅当服务位于 Wcf 服务库 中时,Discover 才有效。但是一旦我转移到其他项目(ConsoleClass 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 服务库,您才可以使用Add Service Reference->Discover 按钮创建客户端,而无需显式运行主机,并且它会找到服务。

如果您创建的类库项目将包含您的服务文件,那么如果服务未托管(是未运行)。你应该运行主机,只有在它把服务地址放在地址栏并按Go

之后

关于c# - 为什么在 "Windows Class Library"项目中创建时未发现 wcf 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11432876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com