locked
Dynamically invoke wcf services RRS feed

  • Question

  • Hello,

    I want to call WCF services dynamically without adding any service refrences and without adding any DTO of the service.

    I have 2 information URL of WCF services and Method name and I have to create SOAP message dynamically.

    For that I have write the following code but "Call WCF Services code" is not working.

    It is giving error on "Message ret = irc.Request(m);"  line.

    I have check "XMLReader" also not showing any value.

    Call WCF Services code

    BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
                IChannelFactory<IRequestChannel> factory = binding.BuildChannelFactory<IRequestChannel>(new BindingParameterCollection());
                factory.Open();

                EndpointAddress address =
                    new EndpointAddress("http://localhost:65457/Service1.svc");
                IRequestChannel irc = factory.CreateChannel(address);
                try
                {
                    using (irc as IDisposable)
                    {
                        irc.Open();
                        StringReader sr = new StringReader(@"<GetData xmlns='http://tempuri.org/'><composite xmlns:a='http://schemas.xmlsoap.org/soap/envelope/' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'><a:value>10</a:value></composite></GetData>");
                        XmlReader reader = XmlReader.Create(sr);
                        Message m = Message.CreateMessage(MessageVersion.Soap11, "http://tempuri.org/IService1/GetData", reader);
                        Message ret = irc.Request(m);
                        reader.Close();

                        Console.WriteLine(ret);
                    }
                }
                catch (Exception ex)
                { }

                //close the factory
                factory.Close();

    Services created as follows

     [ServiceContract]
        public interface IService1
        {

            [OperationContract]
            string GetData(int value);

            [OperationContract]
            CompositeType GetDataUsingDataContract(CompositeType composite);

            // TODO: Add your service operations here
        }
    [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; }
            }
        }
    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;
            }
        }

     Please anybody help me on this issue, what is wrong in my code.

    Thanks in advances

    Bhushan Deshmukh

    Wednesday, September 8, 2010 7:00 AM

Answers

  • Hello,

    Yesterday I have tried a lot to resolve this issue, after some trial & error, I have found that I am calling that function from silverlight  application project  that is wrong.

    I have to call that function either in web project or in normal project.

    thanks for you support.

     

    Regards

    Bhushan Deshmukh

    • Proposed as answer by Surani Thursday, September 9, 2010 5:26 AM
    • Marked as answer by Allen Chen - MSFT Friday, September 10, 2010 2:04 AM
    Thursday, September 9, 2010 4:31 AM

All replies

  • What error do you get?

    If you were able to send something on the network then use a tool like Fiddler to see how it looks like and compare it to what you want to send.


    http://webservices20.blogspot.com/
    WCF Security, Interoperability And Performance Blog
    Wednesday, September 8, 2010 9:46 AM
  • Hello I am getting following error when following statement executes

     Message ret = irc.Request(m);

    {System.NotImplementedException: The method or operation is not implemented.
       at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
       at System.ServiceModel.Channels.RequestChannel.Request(Message message)
       at SilverlightApplication1.MainPage.button1_Click(Object sender, RoutedEventArgs e)}

    Thanks in advances

    Bhushan Deshmukh

     

    Wednesday, September 8, 2010 10:11 AM
  • I was able to get this code working. It is possible that this error comes from the servicer. Verify this using a tool like Fiddler, and if it is the case check the service.
    http://webservices20.blogspot.com/
    WCF Security, Interoperability And Performance Blog
    Wednesday, September 8, 2010 10:54 AM
  • Hello,

    Yesterday I have tried a lot to resolve this issue, after some trial & error, I have found that I am calling that function from silverlight  application project  that is wrong.

    I have to call that function either in web project or in normal project.

    thanks for you support.

     

    Regards

    Bhushan Deshmukh

    • Proposed as answer by Surani Thursday, September 9, 2010 5:26 AM
    • Marked as answer by Allen Chen - MSFT Friday, September 10, 2010 2:04 AM
    Thursday, September 9, 2010 4:31 AM