将类添加到 WCF 服务库 [英] Adding classes to WCF service library

查看:33
本文介绍了将类添加到 WCF 服务库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个定义需要在两个独立应用程序之间共享的事务的类.它们都引用了这个库,并且可以将该类用作数据类型,但不能调用它的任何方法:

I have a class that defines a transaction that needs to be shared between two separate applications. They both have references to this library and can use the class as a data type, but cannot invoke any of its methods:

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

namespace ServerLibrary
{
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        [OperationContract]
        bool ProcessTransaction(Transaction transaction);
    }

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

    // Transaction class to encapsulate products and checkout data
    [DataContract]
    public class Transaction
    {
        [DataMember]
        public int checkoutID;
        [DataMember]
        public DateTime time;
        [DataMember]
        public List<Product> products;
        [DataMember]
        public double totalPrice;
        [DataMember]
        public bool complete;

        public void Start(int ID)
        {
            checkoutID = ID;
            products = new List<Product>();
            complete = false;
        }

        public void Complete()
        {
            time = DateTime.Now;
            complete = true;
        }
    }
}

我做错了什么?

[更新] 我错过了其余的服务.

[UPDATE] I missed out the rest of the service.

谢谢.

推荐答案

为了在客户端和服务器使用相同的 .NET 类型,您需要做的是将数据协定类添加到共享程序集客户端和主机都使用.然后,当您的主机启动并运行并且您添加服务引用时,应该有一个复选框显示重用现有程序集中的类型.

In order to use the same .NET types from both the client and the server what you need to do is to add the data contract classes to a shared assembly that both the client and the host use. Then when your host is up and running and you do an add service reference there should be a checkbox that says reuse types from existing assembly.

这将使 WCF 重新创建您的对象以及您期望的方法和数据.

This will make WCF recreate your objects and with the methods and data that you are expecting.

这篇关于将类添加到 WCF 服务库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆