Compact Framework 与 WCF 通信,无需 NETCFSvcUtil 生成代理

发布于 2024-09-24 15:22:12 字数 1897 浏览 0 评论 0原文

是否可以在不使用 NETCFSvcUtil 生成代理的情况下与 WCF 服务进行通信?

我正在尝试使用紧凑框架在移动设备上调用 WCF 服务。 出于特定原因,我不想使用 NETCFSvcUtil 生成代理

我在移动端有这个示例代码来测试它:

    // This is the contract defined at the client side (mobile device)
    public interface IService1
    {
        string GetData(int value);
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string address = "http://192.168.30.88:8731/Design_Time_Addresses/WcfServiceLibrary1/Service1/";

                // It throws here ....
                var channelFactory = CreateDefaultBinding().BuildChannelFactory<IService1>(new System.ServiceModel.Channels.BindingParameterCollection());
                var channel = channelFactory.CreateChannel(new EndpointAddress(new Uri(address)));
                var result = channel.GetData(5);
                this.Text = result.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        public static System.ServiceModel.Channels.Binding CreateDefaultBinding()
        {
            System.ServiceModel.Channels.CustomBinding binding = new System.ServiceModel.Channels.CustomBinding();
            binding.Elements.Add(new System.ServiceModel.Channels.TextMessageEncodingBindingElement(System.ServiceModel.Channels.MessageVersion.Soap11, System.Text.Encoding.UTF8));
            binding.Elements.Add(new System.ServiceModel.Channels.HttpTransportBindingElement());

            return binding;
        }
    }

但我收到此异常:请求了通道类型“CallingWCFFromDevice.IService1”,但绑定“CustomBinding”不支持它或未正确配置以支持它。 参数名称:TChannel

有任何线索知道发生了什么吗?我猜我没有正确使用 BuildChannelFactory ...

Is it possible to communicate with a WCF service without using NETCFSvcUtil to generate the proxy?

I'm trying to call a WCF service on a mobile using compact framework. I do not want to use NETCFSvcUtil to generate a proxy for a particular reason.

I have this sample code on the mobile side to test it out :

    // This is the contract defined at the client side (mobile device)
    public interface IService1
    {
        string GetData(int value);
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string address = "http://192.168.30.88:8731/Design_Time_Addresses/WcfServiceLibrary1/Service1/";

                // It throws here ....
                var channelFactory = CreateDefaultBinding().BuildChannelFactory<IService1>(new System.ServiceModel.Channels.BindingParameterCollection());
                var channel = channelFactory.CreateChannel(new EndpointAddress(new Uri(address)));
                var result = channel.GetData(5);
                this.Text = result.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        public static System.ServiceModel.Channels.Binding CreateDefaultBinding()
        {
            System.ServiceModel.Channels.CustomBinding binding = new System.ServiceModel.Channels.CustomBinding();
            binding.Elements.Add(new System.ServiceModel.Channels.TextMessageEncodingBindingElement(System.ServiceModel.Channels.MessageVersion.Soap11, System.Text.Encoding.UTF8));
            binding.Elements.Add(new System.ServiceModel.Channels.HttpTransportBindingElement());

            return binding;
        }
    }

But I'm getting this exception : Channel type 'CallingWCFFromDevice.IService1' was requested, but Binding 'CustomBinding' doesn't support it or isn't configured properly to support it.
Parameter name: TChannel

Any clue to what is going on? I'm guessing i'm not using the BuildChannelFactory correctly ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗心人 2024-10-01 15:22:12

我不确定您对安全性有什么要求,但使用预定义的绑定之一可能更简单,例如 BasicHttpBinding;这将使您的代码类似于以下内容:

        IService1 channel = null;
        try {
            BasicHttpBinding binding = new BasicHttpBinding();
            EndpointAddress address = new EndpointAddress("http://192.168.30.88:8731/Design_Time_Addresses/WcfServiceLibrary1/Service1/");
            IChannelFactory<IService1> factory = binding.BuildChannelFactory<IService1>(new BindingParameterCollection());
            channel = factory.CreateChannel(address);
            // Use the channel here...
        }
        finally {
            if(channel != null && ((ICommunicationObject)channel).State == CommunicationState.Opened) {
                ((ICommunicationObject)channel).Close();
            }
        }

在 IService1 的定义中,您不包含任何必要的属性,该接口需要对其应用 ServiceContractAttribute,并且该接口中的方法需要对其应用 OperationContractAttribute,如下所示:

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

I'm not sure what requirements you have for security but it might be simpler to use one of the pre-defined bindings such as BasicHttpBinding; which would make your code something like the following:

        IService1 channel = null;
        try {
            BasicHttpBinding binding = new BasicHttpBinding();
            EndpointAddress address = new EndpointAddress("http://192.168.30.88:8731/Design_Time_Addresses/WcfServiceLibrary1/Service1/");
            IChannelFactory<IService1> factory = binding.BuildChannelFactory<IService1>(new BindingParameterCollection());
            channel = factory.CreateChannel(address);
            // Use the channel here...
        }
        finally {
            if(channel != null && ((ICommunicationObject)channel).State == CommunicationState.Opened) {
                ((ICommunicationObject)channel).Close();
            }
        }

In your definition of IService1 you do not include any of the attributes necessary, the interface needs the ServiceContractAttribute applied to it, and the method in the interface needs the OperationContractAttribute applied to it like so:

[ServiceContract] public interface IService1
{
    [OperationContract] string GetData(int value);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文