Mar 15 2013

Comment by Seth Spearman on Why is a servicestack service routing to GET instead of PUT

Eli...sharp eyes. They were just typos though. I have fixed them in the question.
Mar 15 2013

Why is a servicestack service routing to GET instead of PUT

I was given permission to study ServiceStack this week. I love it. It is an amazing framework. But I have run into a situation where I cannot get a fairly straight-forward example to work. (Although it is admittedly not as straightforward as the examples but is probably more realistic an example.) Apologies in advance for this long question. I have a simple DTO that maps to a database like this...
[Description("Customer")]
[Alias("Customers")]
    public class Customer : IHasId<int>
    {
        [Alias("Id")]
        [AutoIncrement]
        public int Id { get; set;}

        [Required]
        public int CompanyId { get; set;}

        [Required]
        public string FirstName { get; set;}

        [Required]
        public string LastName { get; set;}

        public string MiddleInitial { get; set;}
        public string EmployerName { get; set;}
        public string ServiceLocationDescription { get; set;}
        public string Street1 { get; set;}
        public string Street2 { get; set;}
        public string City { get; set;}
        public string State { get; set;}
        public string Zip { get; set;}

        [Required]
        public string Phone { get; set;}
        public string Fax { get; set;}

        [Required]
        public string EmailAddress { get; set;}
    }
}
I have also created Request DTOs that look like this...
//request dto
[Route("/customers/{companyId/customer/{customerId}", "GET")]
public class GetCustomer  : Customer
{
}

[Route("/customers/{companyId}/customer/{customerId}", "PUT")]
public class UpdateCustomer  : Customer
{
}
I realize the routes are the same...thats probably the issue...but I am designating different http methods.... Finally I have a service that looks like this...
public CustomerResponse Get(GetCustomer request)
{
    return new CustomerResponse { Customer = customerRepository.GetCustomer(request.CustomerId), };
}

public object Put(UpdateCustomer request)
{
    customerRepository.UpdateCustomer(request);
    return new HttpResult
    {
        StatusCode = HttpStatusCode.NoContent,
        Headers = {
            { HttpHeaders.Location, this.RequestContext.AbsoluteUri.CombineWith(request.Id.ToString()) }
        }
    };
}
So to test it out I created the following simple html...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="en-us" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>

<body>

<form action="http://localhost:8080/co/1/customers/1000" method="get">
    <br />
    <label id="Label1">CompanyId&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input name="CompanyId" type="text" /></label><br />
    FirstName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input name="FirstName" type="text" /><br />
    LastName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input name="LastName" type="text" /><br />
    Middle Initial&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    **OTHER FIELDS**  
    <input type="submit" />
</form>

</body>

</html>
All of this is working great only the PUT is routing to the GET service. My goal is to update the customer row with new values of course. I am not showing the customer repository class but that is working fine. I guess. I have a specific an general questions. How do I route to the PUT intstead of the GET. And is there a "best practice" for using the service to do an update. For example...should the PUT service not recieve a customer object but rather all of the values...then the repo code fetches the record and does the udpate? The POST method (not shown) works great BTW. It is exactly like the PUT method (recieves a Customer object etc) EDIT I also just determined that my attempt to use DELETE http method also routes to GET. That is a simple type that does not even inherit from Customer. It just get two delete parameters. Now I am really baffled. EDIT 2 It seems to only be routing to service methods that return concrete types. The exception is the POST which returns object...Get returns a customer reponse object. Get Customers returns a Customers(plural)Response object and works. The rest of the service methods are returning object. Is that it?
Mar 15 2013

Why is a servicestack service routing to GET instead of PUT

I was given permission to study ServiceStack this week. I love it. It is an amazing framework. But I have run into a situation where I cannot get a fairly straight-forward example to work. (Although it is admittedly not as straightforward as the examples but is probably more realistic an example.)

Apologies in advance for this long question.

I have a simple DTO that maps to a database like this...

[Description("Customer")]
[Alias("Customers")]
    public class Customer : IHasId<int>
    {
        [Alias("Id")]
        [AutoIncrement]
        public int Id { get; set;}

        [Required]
        public int CompanyId { get; set;}

        [Required]
        public string FirstName { get; set;}

        [Required]
        public string LastName { get; set;}

        public string MiddleInitial { get; set;}
        public string EmployerName { get; set;}
        public string ServiceLocationDescription { get; set;}
        public string Street1 { get; set;}
        public string Street2 { get; set;}
        public string City { get; set;}
        public string State { get; set;}
        public string Zip { get; set;}

        [Required]
        public string Phone { get; set;}
        public string Fax { get; set;}

        [Required]
        public string EmailAddress { get; set;}
    }
}

I have also created Request DTOs that look like this...

//request dto
[Route("/customers/{companyId/customer/{customerId}", "GET")]
public class GetCustomer  : Customer
{
}

[Route("/customers/{companyId}/customer/{customerId}", "PUT")]
public class UpdateCustomer  : Customer
{
}

I realize the routes are the same...thats probably the issue...but I am designating different http methods....

Finally I have a service that looks like this...

public CustomerResponse Get(GetCustomer request)
{
    return new CustomerResponse { Customer = customerRepository.GetCustomer(request.CustomerId), };
}

public object Put(UpdateCustomer request)
{
    customerRepository.UpdateCustomer(request);
    return new HttpResult
    {
        StatusCode = HttpStatusCode.NoContent,
        Headers = {
            { HttpHeaders.Location, this.RequestContext.AbsoluteUri.CombineWith(request.Id.ToString()) }
        }
    };
}

So to test it out I created the following simple html...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="en-us" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>

<body>

<form action="http://localhost:8080/co/1/customers/1000" method="get">
    <br />
    <label id="Label1">CompanyId&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input name="CompanyId" type="text" /></label><br />
    FirstName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input name="FirstName" type="text" /><br />
    LastName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input name="LastName" type="text" /><br />
    Middle Initial&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    **OTHER FIELDS**  
    <input type="submit" />
</form>

</body>

</html>

All of this is working great only the PUT is routing to the GET service.

My goal is to update the customer row with new values of course.

I am not showing the customer repository class but that is working fine. I guess. I have a specific an general questions.

How do I route to the PUT intstead of the GET. And is there a "best practice" for using the service to do an update. For example...should the PUT service not recieve a customer object but rather all of the values...then the repo code fetches the record and does the udpate?

The POST method (not shown) works great BTW. It is exactly like the PUT method (recieves a Customer object etc)

EDIT

I also just determined that my attempt to use DELETE http method also routes to GET. That is a simple type that does not even inherit from Customer. It just get two delete parameters. Now I am really baffled.

EDIT 2

It seems to only be routing to service methods that return concrete types. The exception is the POST which returns object...Get returns a customer reponse object. Get Customers returns a Customers(plural)Response object and works. The rest of the service methods are returning object. Is that it?

Mar 13 2013

Comment by Seth Spearman on Is there a yet fairly complete example projects servicestack that uses the new API?

I thought I would add...that you can still use SVN to get GitHub sources. I just did an svn co from github.com/ServiceStack/ServiceStack.Examples/trunk and it worked great. (No dvcs goodness for me...yet).
Mar 13 2013

Comment by Seth Spearman on Is there a yet fairly complete example projects servicestack that uses the new API?

OK... will do that.
Mar 13 2013

Comment by Seth Spearman on Is there a yet fairly complete example projects servicestack that uses the new API?

I downloaded from this link... github.com/downloads/ServiceStack/ServiceStack.Examples/… and it definitely is using the old API. The link you referred to is up to date. Am I using the wrong download link. Seth
Mar 13 2013

Comment by Seth Spearman on Is there a yet fairly complete example projects servicestack that uses the new API?

I downloaded from this link... github.com/downloads/ServiceStack/ServiceStack.Examples/… and it definitely is using the old API. The link you referred to is up to date. Am I using the wrong download link. Seth
Mar 13 2013

Comment by Seth Spearman on Is there a yet fairly complete example projects servicestack that uses the new API?

mythz...thanks for your answer. amazing work you have done. for the record I have looked at the MovieRest example and the Northwind example, and it seems to me that both of them are using the old api? OnGet instead of Get, RestSErviceBase instead of Service, etc. Can you mention a specific example from the suite that is updated?
Mar 13 2013

Is there a yet fairly complete example projects servicestack that uses the new API?

I got the week off from work to learn servicestack and I am in awe of its simplicity, power, and speed.

I am a pluralsight subscriber and I am going through the Jon Somnez course which is great and as far as I can tell is using the new service stack api.

However, the examples in the pluralsight course are extremely simple and as such I am looking for a working example THAT USES the new API. I see that the wiki is up to date and that is very helpful...but I was hoping to find a working implementation that uses the new API and it seems that all of the examples I have downloaded or browsed are using the old API.

IS there a new working example/implementation (vs solution and projects.) that uses the new API yet?

If I can sneak in another question. Is there a recommendation (hopefully in the example project you can point me to) that explains a best practice for structuring your servicestack API project. (DTOs in a folder, services in a folder, response objects in a folder ??)

Thanks.

Mar 13 2013

Is there a yet fairly complete example projects servicestack that uses the new API?