jeff
jeff

Reputation: 684

ServiceStack with Protobuf format

I am trying to use protobuf format in ServiceStack Webservices ( following the example at ServiceStack: REST with ProtoBuf by Steven Hollidge. I have added a Winform application to consume the webservice. The codes are given below.

HelloService.cs

using System.Runtime.Serialization;
using ProtoBuf;
using ServiceStack.Demo.Rest;
using ServiceStack.ServiceHost;
using ServiceStack.ServiceInterface;

namespace ServiceStack.Demo.WebService
{
[DataContract] 
public class Hello
{
    [DataMember(Order = 1)]
    public string Name { get; set; }
}
 [DataContract] 
public class HelloResponse
{
    [DataMember(Order = 1)]
    public string Result { get; set; }
}

 public class HelloService : RestServiceBase<Hello>
{
    public  override object OnGet(Hello request)
    {
        return new HelloResponse { Result = "Hello, " + request.Name };
    }
}
}

Global.asax.cs

using System;
using System.Web;
using Funq;
using ServiceStack.Demo.Rest;
using ServiceStack.Demo.WebService;
using ServiceStack.WebHost.Endpoints;

namespace ServiceStack.Demo
{
public class AppHost : AppHostBase
{
    public AppHost() : base("ServiceStack makes services easy!", typeof (AppHost).Assembly)
    {
        ServiceStack.Plugins.ProtoBuf.AppStart.Start();
    }

    public override void Configure(Container container)
    {
        Routes
          .Add<Hello>("/hello")
          .Add<Hello>("/hello/{Name}");

    }
 }

public class Global : HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        new AppHost().Init();
    }
}
}

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ServiceStack.ServiceClient;
using ProtoBuf;
using ServiceStack.Plugins.ProtoBuf;
using System.Runtime.Serialization;
using ServiceStack.ServiceClient.Web;

namespace client
{
public partial class Form1 : Form
{
    private ServiceClientBase _client;
    private const string Url = "http://localhost/servicestack.demo/servicestack/hello?format=x-protobuf";
    public Form1()
    {
        InitializeComponent();
    }

    private void Button1Click(object sender, EventArgs e)
    {

        this._client =
            new ProtoBufServiceClient(Url);

        var response = _client.Send<HelloResponse>(new Hello {Name = "ProtoBuf"});
        label1.Text = response.Result;
    }

   
    public class Hello
    {
        public string Name { get; set; }
    }

    
    public class HelloResponse
    {
        public string Result { get; set; }
    }
}
}

I am getting System.InvalidOperationException: Type is not expected, and no contract can be inferred: client.Form1+Hello

What am I doing wrong? Please suggest.....

Upvotes: 2

Views: 1616

Answers (3)

jeff
jeff

Reputation: 684

I have updated the Form1.cs to the following and now it is working fine( refer to http://upjnv.blogspot.in/

 using System;  
 using System.Windows.Forms;  
 using ServiceStack.Plugins.ProtoBuf;  
 using System.Runtime.Serialization;  
 using ServiceStack.ServiceClient.Web;  

namespace client  

{  

 public partial class Form1 : Form  

 {  

private ServiceClientBase _client;  

private const string Url = "http://localhost/servicestack.demo/servicestack/";  

public Form1()  

{  

  InitializeComponent();  

}  

private void Button1Click(object sender, EventArgs e)  

{  

  this._client =  

    new ProtoBufServiceClient(Url);  

  var response = _client.Send<HelloResponse>("GET","/hello",new Hello {Name = "ProtoBuf"});  

  label1.Text = response.Result;  

 }  

 }  

[DataContract]  

public class Hello  

{  

[DataMember(Order = 1)]  

public string Name { get; set; }  

}  

[DataContract]  

public class HelloResponse  

 {  

[DataMember(Order = 1)]  

public string Result { get; set; }  

 } 



 }

Upvotes: 1

paaschpa
paaschpa

Reputation: 4816

It looks like you have your Hello class and your HelloResponse class declared twice. Once in HelloService.cs and again as inner classes in Form.cs. Removing the duplicates from your Form.cs file should allow your ProtoBufServiceClient to reference the correct classes/types.

Upvotes: 2

Martin Tapp
Martin Tapp

Reputation: 3396

Put your POCO classes in the same namespace, that should do it.

Upvotes: 0

Related Questions