views:

514

answers:

5

I have the following test method:

Imports System.Web

Imports System.Web.Services

Imports System.Web.Services.Protocols


<WebService(Namespace:="http://tempuri.org/")&gt; _

<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _

<System.Web.Script.Services.ScriptService()> _

Public Class DemoService
 Inherits System.Web.Services.WebService

<WebMethod()> _
Public Function GetCustomer() As String
    Return "Microsoft"
End Function


End Class

Even with the ResponseFormat attribute added the response is still being returned as XML rather than JSON.

Thoughts appreciated.

+2  A: 

Do you have .NET 3.5 or greater installed?

ScriptServiceAttribute is in .NET 3.5 and 4.0.

Also, clear your ASP.NET temp files, the dynamic proxy could be cached.

rick schott
Target is .NET 3.5.Cleared temp files and still get same response.
ChrisP
Your code looks solid, try changing your ScriptService tag to this, <ScriptService()> _.
rick schott
A: 

This is what I do, though there is probably a better approach, it works for me:

[WebMethod]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public string retrieveWorkActivities(int TrackNumber)
{
            String s = {'result': 'success'};
            return s.ToJSON();
}
James Black
Don't you mean `String s = "{'result': 'success'}";` ? And obviously you've posted C# and the question was VB.Net (I know everyone does that)
MarkJ
@markJ - I didn't have an example in VB.NET, but I was mainly showing the annotations, but I didn't think about the language either. And I removed the double quote. Thanx.
James Black
A: 

Why not just use an ashx file? It's a generic handler. Very easy to use and return data. I use these often in place of creating a web service as they are much lighter.

An example of the implementation in the ashx would be:

// ASHX details
DataLayer dl = GetDataLayer();
List<SomeObject> lst = dl.ListSomeObjects();
string result = "";
if (lst != null)
{
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    result = serializer.Serialize(lst);
}
context.Response.ContentType = "application/json";
context.Response.Write(result);
context.Response.End();

If you do need to use a web service though you could set the ResponseFormat. Check out this SO question that has what you are looking for:

http://stackoverflow.com/questions/211348/how-to-let-an-asmx-file-output-json

Kelsey
@chrisp Did you ever get your issue sorted out? If this answer or another helped you, you should set an accepted answer so that others reading this question can locate the solution that helped you.
Kelsey
A: 

If you're restricted to the 2.0 Framework, you can use the JavaScriptSerializer, from the System.Web.Extensions assembly, like this (in C#):

[WebMethod()]
[ScriptMethod()]
public static string GetJsonData() {
    // data is some object instance
    return new JavaScriptSerializer().Serialize(data);
}
Jeff Sternal
A: 

I've used ashxes for this problem too. To be honest I didn't know webservices had that ResponseFormat attribute. That said I think I still prefer the ashx route for lightness and control.

There's some peripheral details left out here, to concentrate on the bit you'd need.

    Imports Newtonsoft.Json
    Imports Newtonsoft.Json.Linq

    Namespace Handlers.Reports

        Public MustInherit Class Base
            Implements IHttpHandler, SessionState.IRequiresSessionState


            Protected data As String()() = Nothing
            Private Shared ReadOnly JsonContentType As String = "application/json" 

          Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest

                Try

                    Me.GetData()
                    Me.BuildJsonResponse(context)

                Catch ex As Exception

                End Try

                context.Response.End()

            End Sub

     Private Sub BuildJsonResponse(ByVal context As System.Web.HttpContext)

                context.Response.AddHeader("Content-type", Base.JsonContentType)

                Dim json = Me.BuildJson()
                context.Response.Write(json)

            End Sub

            Private Function BuildJson() As String

                If Not Me.data Is Nothing Then

                    Return String.Format("{{data: {0}, pageInfo: {{totalRowNum:{1}}}, recordType: 'array'}}", JsonConvert.SerializeObject(Me.data), Me.totalRows)

                End If

                Return String.Empty

            End Function
 End Class

End Namespace
Mark Holland