This question already has answers here:
How to debug a web service in a C#/.NET solution from a web application
(5 answers)
Closed 8 years ago.
I am using visual studio and I have asp.net application as one project and a web service as another project.I am using web service in my asp.net application. There is some sort of problem im my webservice code.But i am unable to debug continuosly from asp.net application to web service.I put break point both in application and web service but break point not activated in web service and it shows me connection error.How can i do this while hosting on localhost?
If you're running web application as startup project, try running web service in another debug instance.
You can do it by right-clicking on web service project, Debug -> Start new instance
You should attach the debugger to w3wp (IIS process).
Here is a link that could help you.
If you want to debug in local system, You can set multiple start up projects.
You can set multiple startup by Solution properties.
Hope this help
Try to debug the service itself and see if it hits breakpoint. Just set the project that has service in it to be the main project and set the service to be the main start page.
If it doesn't hit the breakpoint it probably didn't load all the symbols. That happens if the project is set to, lets say, Release configuration and not Debug.
Is the web service running on a remote computer , if so you need to setup remote debug for the web service.
Can You please check that you add Service reference your web service or not other you can't access your web service function. I am useing web service in my project like this it's below
this is my web service code
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class JsonData : System.Web.Services.WebService
{
[WebMethod(Description = "")]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public StateData[] GetStateByCountryID(int ID)
{
StateData objStateData = new StateData();
LMGDAL.db_LMGEntities dbData = new db_LMGEntities();
var data = (from con in dbData.tblStates
where con.State_CountryID == ID
select new StateData
{
StateID = con.StateID,
StateName = con.StateName
}).ToList();
return data.ToArray();
}
then i add Service reference to my asp.net web form
this code in my form
<script type="text/javascript">
$(function () {
$("#ddlCountry").change(function () {
var countryID = $("#ddlCountry").val();
$.ajax({
type: "POST",
url: "JsonData.asmx/GetStateByCountryID",
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: '{ID:"' + countryID + '"}',
success: function (msg) {
var data = msg.d;
var stateData = "";
$.each(data, function (index, itemdata) {
stateData += "<option value='" + itemdata.StateID + "' > " + itemdata.StateName + " </option>";
});
$("#ddlState").empty();
$("#ddlState").append("<option value='0'>-Select State-</option>");
$("#ddlState").append(stateData);
},
error: function () {
alert('Faild To Retrieve States.');
}
});
});
I think this will help you
Related
I use "/Controller/ActionName" url format for my ajax calls. This pattern works fine with my development server that has this pattern
/Controller/ActionName
My application has been deployed to IIS with the following url pattern
/domain/Controller/ActionName
So whenever I call a resource it routes to /Controller/ActionName instead of /domain/Controller/ActionName there by resulting in 404 error. Is there any quick fix for this by modifying the routeconfiguration such that it works on both the devl as well as the IIS.
If you defining the relative urls "manually" as a string try not using a '/' in the beginning.
"Controller/ActionName"
Edit
What I've done the the past is included a Partial Razor View that is responsible of setting urls using #Url.Action()
Partial - Shared/JavascriptUrlsPartial.cshtml
<script type="text/javascript">
(function ($, app) {
app.urls =
{
actionA: '#Url.Action("Controller", "ActionA")',
};
})(jQuery, app = app || {});
</script>
Then you reference the rendered url in javascript.
$.ajax({
url: app.urls.actionA,
//etc
})
This partial is then included in the _Layout.cshtml.
In my Razor view, we have Ajax calls like:
var model = { searchId: searchId, newSearchName: searchName, newSearchDescription: description };
$.ajax({
url: '#Url.Action("Rename", "Search")',
contentType: 'application/json; charset=utf-8',
type: 'POST',
dataType: 'html',
data: JSON.stringify(model)
})
We have not done anything special in local or production environment. We host the application on IIS 7.5.
I could not make out what's different in your case from your question and comments. If this does not solve the problem please add more information around your setup.
Edit (7-17-2014) It's been four days since I posted this question and I am still stumped. Can anyone offer suggestions or places where I can start looking? Based on what I've learned so far, it seems possibly VS 2013 is looking in a different location than VS 2012...could that be it...I need to modify my web service path?
I created a web application in VS 2012, that uses web services. Everything works as it should. I just upgraded to VS 2013 and now my web services gives an error.
When I check the result object, in the resultText, I see the error:
No web service found at: /testserver/WebServices.asmx
Not sure how to approach this since it works in VS 2012. Can someone help?
Thank you.
WebServices.asmx file:
<%# WebService Language="C#" CodeBehind="~/App_Code/WebServices.cs"
Class="WebServices.Services" %>
WebServices.cs Signature:
namespace WebServices
{
[WebService(Namespace = "http://MyCompanyInhouseserver.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
[ScriptService]
public class Services : WebService
{
Code...
}
}
Ajax: (Added this in case it is a coding issue that got exposed in VS 2013. Code breaks in the complete: section.)
var pageUrl = "/testserver/WebServices.asmx";
$.ajax({
type: "POST",
url: pageUrl + "/ValidateLogin",
data: "{'username': '" + self.username() + "', 'password': '" + self.password() + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(result) {
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
},
complete: function (result) {
if (result.responseJSON.d != null && result.responseJSON.d.UserId > 0) {
$(window.location).attr('href', 'startproject.aspx');
} else {
self.loginError("Invalid username/password");
}
}
});
Check the web project's properties in visual studio and go to the Web section. Determine if you are using Local IIS which will require you to Create a Virtual Directory as well as have IIS installed. If you have IIS Express you will also need to create a virtual directory; but note that off of the localhost is a port number, you may need to adjust your url to that port number as well as local host for testing purposes after you build and run the project.
I have an WebService in C# and I want to access this webservice from another application.
Ex. Have one webservice running in localhost and i also have a website running in localhost, and this two projects are in diferents places. The question is: How do I invoke this webservice from my website with ajax, both in localhost.
The Code that i have is this:
WebService
[System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public String HelloWorld()
{
return "Hello World";
}
}
and Client
$.ajax({
type: "POST",
url: "localhost:52137/Service1.asmx?op=HelloWorld",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: '',
success: function (data, status) {
alert(data.d);
},
error: function(data, status){
alert(status);
}
});
Try changing url to this:
url: "http: //localhost:52137/Service1.asmx/HelloWorld"
And BTW.. if the website is running on a different port than the service.. you still have xdomain issues.
CORS ASMX
As these two projects are in different places, it would be CORS request.
You need to enabled cross domain request in your service application as suggested in this article.
You can use $.getJSON which allow cross domain request.
As you're using C# you can create HTTP Handler as shown in this article.
I have two web projects in Visual Studio 2008 SP1, One hosting a WCF service and another that I am using to consume the WCF service. The service in question exposes a webHttpBinding endpoint and take JSON as input and returns JSON.
I have used Jquery ajax to consume the service from the client web app like so:
$(document).ready(function () {
var data = '{"myobject":{"Business":"PIZZA"}}';
$.ajax({
type: "POST",
contentType: "application/json",
data: data,
dataType: 'json',
url: "http://localhost:1212/JobInfo.svc/ReturnThisString",
error:
function(XMLHttpRequest, textStatus, errorThrown) {
alert("Error");
},
success: function(data) {
alert("BoolValue: " + data.GetDataUsingDataContractResult.BoolValue);
alert("StringValue: " + data.GetDataUsingDataContractResult.StringValue);
}
});
});
And I get the following error in the Firebug HTML request:
http://localhost:1212/JobInfo.svc/ReturnThisString 405 Method Not Allowed
But if run the exact same file in the web project that is hosting the project it works fine.
The hosting project is running on set port localhost:1212 and the client on localhost:RANDOM_PORT. As the localhost is the same I would imagine its not a Cross Domain issue? Or does the port part count also?
The server contract is as follows:
[OperationContract]
[WebInvoke(Method="POST",UriTemplate="ReturnThisString",
BodyStyle=WebMessageBodyStyle.Wrapped,
RequestFormat=WebMessageFormat.Json,
ResponseFormat=WebMessageFormat.Json)]
EventListArgs ReturnThisString(EventListArgs myobject);
[Serializable]
[DataContract]
public class EventListArgs {
[DataMember( IsRequired = false)]
public string Business;
[DataMember( IsRequired = false)]
public string Feeder;
}
And the implemented code is:
public EventListArgs ReturnThisString(EventListArgs myobject)
{
return myobject;
}
This was added as a simple method to prove I had not just done something silly. What I like to call a sanity check. Its a simple function that takes a complex(of type other then string) JSON object as a parameter and returns a complex JSON object.
As you can see above I have two web project that use the Visual Studio Built in web server (local IIS install not possible). The above configuration does not work but if you can see I have also got the Darkside test page in the server host which is an exact copy from the client and this work with no problems at all.
Try using IIS Express
It's got the same features as a full blown IIS and requires no install.
A simple command line is all you need to start it up. It should get rid of the Casini related errors you were getting.
I'm using ajax in my website to call some information from a UserControl called NewsFeed.ascx which is found in the folder 'controls', my way is to make a web service page (in a folder called WebMethods) which contain a function in my case called GetRSSReader which returns a string format:
[WebMethod]
public string GetRSSReader()
{
Page page = new Page();
UserControl ctl =
(UserControl)page.LoadControl("~/Controls/NewsFeed.ascx");
page.Controls.Add(ctl);
StringWriter writer = new StringWriter();
HttpContext.Current.Server.Execute(page, writer, false);
return writer.ToString();
}
then I call this page using Jquery (which I found it too heavy) to get the returned data as a JSON like this :
<div id="Content"></div>
<script type="text/javascript" defer="defer" src="../JAVA/Default.js"></script>
>
$(document).ready(Update);
function Requests()
{
$.ajax({
type: "POST",
url: "../WebMethods/Feed.asmx/GetRSSReader",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$('#Content').html(msg.d);
}
});
}
the Jquery.js and this page (default.js) founded in the folder Java
my question : can I not to use webService and instead using WCF !!! and how !?
What you refer to as "web services" is the old "ASMX Web Service" feature of .NET (sometimes known as ASP.NET Web Services).
WCF is the replacement for ASMX web services.
See https://stackoverflow.com/tags/wcf/info for some getting-started information.
If you're just returning JSON then I highly recommend you simply use an HttpHandler instead of some combination of WCF, SOAP, UserControls, and whatever else you're throwing in there. Here's a quick tutorial on the subject. You'd save yourself the overhead of page life-cycle stuff that you don't need. And returning the JSON is as simple as serializing your return values with the JavaScriptSerializer.