Little help plz

Hi i have the following code taken from a tutorial. Now i wish to return a string called returnString but it won't compile because the string is being accessed from another class.

So how do i return the string

Basically i wish my service to access objects from the application hosting the service.

Thanks.

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;

namespace Messenger
{
public class ClientService
{
ServiceHost serviceHost;
String returnString = "String I want to return";

public ClientService()
{
}

public void startService()
{
Uri baseUri = new Uri("http://localhost:1234/hello");
serviceHost = new ServiceHost(typeof(HelloService), baseUri);
serviceHost.AddServiceEndpoint(typeof(HelloService), new BasicHttpBinding(), baseUri);
serviceHost.Open();
}
}

[ServiceContract]
class HelloService
{
[OperationContract]
string sayHi()
{
return returnString; // won't compile because in it's own class. So how
}
}
}



Answer this question

Little help plz

  • sharpMoon

    watch the typo is [ServiceBehavior] and [OperationBehavior]

  • Daikoku

    >>this.controlContacts.addMessage(message); // won't compile, how do i get access to my controlContracts object


    this refers to the HelloService class and i see no static member controlContacts, so your code doesn't seems to be valid and i don't get exactly what you are trying to achive. Sorry, this doesn't seem to be a WCF issue.

    Good luck.



  • Learning VB

    //you can use IExtension<ServiceHostBase> class to share the data between host and service(when you are designing svc,you have to assume,host may add extensions)

    SAMPLE CODE:

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.ServiceModel;

    using System.Xml;

    using System.Runtime.Serialization;

    namespace ConsoleApplication1

    {

    [ServiceContract]

    public interface IPhotoGet

    {

    [OperationContract]

    string GetPhoto(string name);

    }

    public class PhotoImplement : IPhotoGet

    {

    #region IPhotoGet Members

    public string GetPhoto(string name)

    {

    MyContext context = OperationContext.Current.Host.Extensions.Find<MyContext>();

    return context.m_Name;

    }

    #endregion

    }

    public class MyContext : IExtension<ServiceHostBase>

    {

    public string m_Name;

    public int m_Age;

    public MyContext(string Name, int Age)

    {

    m_Name = Name;

    m_Age = Age;

    }

    #region IExtension<ServiceHostBase> Members

    public void Attach(ServiceHostBase owner)

    {

    //nothing

    }

    public void Detach(ServiceHostBase owner)

    {

    //nothing to implement

    }

    #endregion

    }

    public class Program

    {

    static void Main(string[] args)

    {

    ServiceHost svcHost = new ServiceHost(typeof(PhotoImplement));

    NetTcpBinding tcpBinding = new NetTcpBinding();

    tcpBinding.ReceiveTimeout = new TimeSpan(0, 30, 00);

    tcpBinding.SendTimeout = new TimeSpan(0, 30, 00);

    svcHost.AddServiceEndpoint(typeof(IPhotoGet), tcpBinding, @"net.tcp://localhost/photo");

    svcHost.Extensions.Add(new MyContext("tester",20));

    svcHost.Open();

    Console.WriteLine("service is ready");

    Console.ReadLine();

    }

    }

    }



  • WayneSpangler

    Okay, what kind of data are you trying to pass from your client to server Here are your options.

    1. If you want to pass any .NET buit-in data types then all you need is to pass them as a parameter. For example, if you want to pass a String change your Operation contract as

    string sayHi( string data);

    All scalar types have implicit DataContract.

    2. If you want to pass a custom type then you can

    a. Either make them Serializable by annotating with a [SerializableAttribute] or implement ISerializable or

    b. Annotate your type with a [DataContractAttribute] and the members that need to be serialized with a [DataMemberAttribute]

    Hope this helps.



  • MSJ17

    Ok, how do i do this

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.ServiceModel;

    namespace Messenger
    {
    public class ClientService
    {
    ControlContacts controlContacts;
    ServiceHost serviceHost;

    public ClientService(ControlContacts a_controlContacts)
    {
    this.controlContacts = a_controlContacts;
    }

    public void startService()
    {
    Uri baseUri = new Uri("http://localhost:1234/hello");
    serviceHost = new ServiceHost(typeof(HelloService), baseUri);
    serviceHost.AddServiceEndpoint(typeof(HelloService), new BasicHttpBinding(), baseUri);
    serviceHost.Open();
    }

    public void stopService()
    {
    serviceHost.Close();
    serviceHost = null;
    }
    }
    [ServiceContract]
    class HelloService
    {
    [OperationContract]
    string sayHi()
    {
    return "hi";
    }

    [OperationContract]
    String addMessage(String message)
    {
    String response = this.controlContacts.addMessage(message); // won't compile, how do i get access to my controlContracts object
    return response;
    }
    }

    }


  • AlbertoV

    Or another way, how can i display the data my service receives

    I need to transfer data from the service to my aplication. As it is the service is isolated from my app.


  • arcLee

    The object i am trying to get at is this:

    Private ControlContacts; // This!!

    public ClientService(ControlContacts a_controlContacts)
    {
    this.controlContacts = a_controlContacts;
    }

    My appication is holding the data and hosting the service. How can i get the service to access the data


  • Abualnassr

    Code doesn't look correct. Just add the returnString as a private member.

    [ServiceContract]
    class HelloService
    {

    String returnString = "String I want to return";


    [OperationContract]
    string sayHi()
    {
    return returnString; // won't compile because in it's own class. So how
    }
    }



  • fddsfsdf

    I see, i thought not compiling was the problem. Here is what i would recommend.

    [ServiceContract]
    interface IHelloService
    {
    [OperationContract]
    String sayHi();

    }

    [ServiceBehaviour]
    class HelloService :IHelloService
    {

    String returnString = "String I want to return";


    [OperationBehaviour]
    string sayHi()
    {
    return returnString; // won't compile because in it's own class. So how
    }
    }


    //Client code to call the Service without generating proxy via SVCUTIL.EXE and config files

    EndPointAddress address = new EndpointAddress("http://localhost:1234/hello");
    ChannelFactory<IHelloService> factory = new ChannelFactory<IHelloService>(new BasicHttpBinding(), address);
    IHelloService proxy = factory.CreateChannel();
    proxy.sayHi()



  • Fernando Tubio

    Madhu Ponduru -MSFT wrote:

    all our SDK samples are transfering data between client and service and service and client,please check SDK samples

    http://windowssdk.msdn.microsoft.com/en-us/library/ms750530.aspx

    http://windowssdk.msdn.microsoft.com/en-us/library/ms751519.aspx

    -Thank you

    Madhu

    Those examples are no good to me. I know that info.

    I need my last question answered.

    As it stands the service is ISOLATED from the application hosting the service.

    If i wish to pass data from the application hosting the service to the service itself, how do i do it

    Am i missing something It's a simple question.


  • scuudz

    You have my question wrong.

    How do i get data IN TO HelloService class I wish to use WCF to send messages between clients. so how do i get my messages to my service


  • andyedw

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.ServiceModel;

    namespace Messenger
    {
    public class ClientService
    {
    ControlContacts controlContacts; // I am trying to access this!!!!!!!!!!!!!!!!!
    ServiceHost serviceHost;

    public ClientService(ControlContacts a_controlContacts)
    {
    this.controlContacts = a_controlContacts;
    }

    public void startService()
    {
    Uri baseUri = new Uri("http://localhost:1234/hello");
    serviceHost = new ServiceHost(typeof(HelloService), baseUri);
    serviceHost.AddServiceEndpoint(typeof(HelloService), new BasicHttpBinding(), baseUri);
    serviceHost.Open();
    }

    public void stopService()
    {
    serviceHost.Close();
    serviceHost = null;
    }
    }
    [ServiceContract]
    class HelloService
    {
    [OperationContract]
    string sayHi()
    {
    return "hi";
    }

    [OperationContract]
    String addMessage(String message)
    {
    String response = this.controlContacts.addMessage(message); // won't compile, how do i get access to my controlContracts object
    return response;
    }
    }

    }


  • matthew lyden

    I think you have my question wrong. I wish my service to access data from my application not from the service itself.

    How do i pass in data to the service so my client can connect and receive this data.


  • uksuv

    all our SDK samples are transfering data between client and service and service and client,please check SDK samples

    http://windowssdk.msdn.microsoft.com/en-us/library/ms750530.aspx

    http://windowssdk.msdn.microsoft.com/en-us/library/ms751519.aspx

    -Thank you

    Madhu



  • Little help plz