gpt4 book ai didi

wcf - WCF 服务未公开的类型

转载 作者:行者123 更新时间:2023-12-04 23:33:01 27 4
gpt4 key购买 nike

我有一个小型测试网络服务来模拟我在现实世界应用程序中注意到的一些奇怪的东西。由于演示显示与应用程序相同的行为,为了简洁起见,我将使用演示。

简而言之,我的服务接口(interface)文件如下所示(您可以看到它是 VS2008 创建的默认 WCF 服务,但我添加了一个新的公共(public)方法(GetOtherType())和底部的两个新类(SomeOtherType 和 SomeComplexType)。SomeOtherType管理 SomeComplexType 类型的通用列表

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WCFServiceTest
{

[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);

[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);

[OperationContract]
SomeOtherType GetOtherType();

}


[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; }
}
}

[DataContract]
public class SomeOtherType
{
public List<SomeComplexType> Items { get; set; }
}

[DataContract]
public class SomeComplexType
{

}
}

我的服务实现如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WCFServiceTest
{

public class Service1 : IService1
{


#region IService1 Members

public string GetData(int value)
{
throw new NotImplementedException();
}

public CompositeType GetDataUsingDataContract(CompositeType composite)
{
throw new NotImplementedException();
}

#endregion

#region IService1 Members


public SomeOtherType GetOtherType()
{
throw new NotImplementedException();
}

#endregion
}
}

我遇到的问题是,如果我在 ASP.NET Web 应用程序中包含对此服务的服务引用,我无法通过智能感知看到 SomeComplexType。错误与找不到类型或命名空间有关。但是,可以找到 SomeOtherType (我假设该类型是来自公共(public)方法之一的返回类型)。

如果我的公共(public)方法之一(返回类型或参数)的方法签名中没有该类型,我是否认为我不能从 WCF 服务公开该类型?如果是这样,我将如何迭代客户端上 SomeOtherType 实例中的项目?

非常感谢,我希望这很清楚。

西蒙

最佳答案

The problem I have is that if I include a service reference to this service in an ASP.NET Web Application, I cannot see SomeComplexType via intellisense. The error relates to the type or namespace cannot be found. However, SomeOtherType can be found (I'm assuming as the type is a return type from one of the public methods).

Am I right in thinking I can't expose a type from a WCF Service if that type is not featured in the method signature of one of my public methods (either return type or argument)? If so, how would I be able to iterate over the Items inside an instance of SomeOtherType on the client?



你是绝对正确的 - 你的 SomeComplexType从未在任何服务方法中使用,并且它也从未在任何确实用作服务方法中的参数的类型中标记为 [DataMember]。因此,从 WCF 的角度来看,它不是必需的,也不会出现在服务的 WSDL/XSD 中。

正如格雷厄姆已经指出的那样 - 您正在使用 SomeComplexType在一个地方:
[DataContract]
public class SomeOtherType
{
public List<SomeComplexType> Items { get; set; }
}

但自从 Items元素未标记为 [DataMember] ,它(以及它使用的类型)将不会包含在您的服务的 WSDL/XSD 中。由于 Items未标记为 DataMember ,它们也不会出现在您的序列化 WCF 消息中,因此您永远不需要遍历此集合 :-)

所以最有可能的是,您真正想要的只是添加 [DataMember]归因于您的 Items属性(property);那么它将被包含在 WSDL/XSD 中, SomeComplexType 也将包含在内。 .

关于wcf - WCF 服务未公开的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2507889/

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