开发者

Compact Framework communication with WCF without NETCFSvcUtil generated proxies

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 supp开发者_JAVA百科ort it. Parameter name: TChannel

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


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜