WCF app.config服务名称在项目之外
本文关键字:项目 app config 服务 WCF | 更新日期: 2023-09-27 18:25:02
我在尝试创建WCF服务时遇到问题。我正在尝试将服务、数据合同和服务合同放在不同的项目中。我一直得到"WCF服务主机找不到任何服务元数据。"错误。这是我的appconfig文件:
<system.serviceModel>
<services>
<service name="SampleServiceContract.Service1">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8732/Service1" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address ="" binding="wsHttpBinding" contract="SampleServiceContract.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>
这是我的IService文件:
// 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; }
}
}
最后是我的Service1文件:
// 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;
}
}
app.config位于命名空间"SampleWCF"下。这也是项目名称。我的IService1和Service1文件位于命名空间"SampleServiceContract"下,这也是项目名称。
提前感谢!
尝试在serviceBehaviors标记下的行为中为您的服务行为命名。并在服务标签的behaviourConfiguration中指定相同的名称。
我终于解决了我的问题。我在ServiceContract中添加了"命名空间",一切似乎都很好!
示例:
[ServiceContract(Namespace = "Services.CountermeasureService")]