I am looking for new wcf rest web service in asp.net 4.0 using vs2010.
here is my code for passing url:
"homewizard/Service1.svc/monthlytips/country_state=US_IL_N/tip_code=NoInformation/feature=ACC,FAHD,WHG,FP,WA,DY,DWSH,GD,REF,STV,OVN,MW,CPTR,ATT,ROOF,RG,BSMT,FDN,SPX,GAR,EGF,PLB,DOOR,WIND,WS,LWN,DKG,PF,BBQ,WSD,OWF,DWY,OLIT,HL,SPTC,CF,WF,CPTS,DVB,FURW,FURL,FURU,MAT,BATH,KITC,CLST,LITE,SD,COD,FE,EMS,PC,SS,MED,EAUD,ENR,GARR,INR,MGR,TAXR,TELR,CGD,DOOR,WIND,WS/dwelling_type=1/tip_priority=1/month=3/tip_knowledge_level=1/tipbr_ensav=0/tipbr_safe=0/tipbr_avoid=1/tipbr_comfort=1/tipbr_value=1/tipbr_appear=1/tipbr_green=0/tipbr_money=0/tipbr_space=1/tipbr_allergy=2/tipbr_elderly=2/tipbr_children=2/tip_location_temp=0/tip_location_humidity=0"
output:Bad Request - Invalid URL HTTP Error 400. The request URL is invalid.
my web config is: httpRuntime maxUrlLength="1024"
but it's working my local host not in server pc.
thanks in advance.
To be honest your issue is that that's not a restfull URL, the url should just be the location of the resource you are dealing with, the body of the request should contain the details of the request (as xml or json for example). It will both solve your problem and result in less code to manage the parameters via deserialization into a request object server side, not to mention clear up that url.
UPDATE
Are you sure thats a GET and not a PUT, what is the call meant to do?
If it is a GET I'd do something like the following...
Service Interface
[WebGet(UriTemplate = "monthlytips")]
AppropriateReturnType MonthlyTips(MonthlyTip monthlytip);
DTO Object
public class MonthlyTip
{
public string CountryState { get; set; }
public string TipCode { get; set; }
public List<string> Feature { get; set; }
public int DwellingType { get; set; }
public int TipPriority { get; set; }
...
...
}
Thats off the top of my head so it might need a little refining and you'll need to implement the interface, finish the DTO and so on but that's the approach you should take.
Related
I am a beginner in programming and have recently learnt and used http uri to get data and then parse json string from the stream using C# in SSIS and was able to load the data into sql server database.
Below is the sample code I used..
System.Uri uri = new Uri("API URL");
WebRequest webRequest = HttpWebRequest.Create(uri);
HttpWebRequest httpWebRequest = (HttpWebRequest)webRequest;
NetworkCredential networkCredential = new NetworkCredential("LOGIN","Password");
credentialCache.Add(uri,"Basic",networkCredential);
WebResponse webResponse = webRequest.GetResponse();
...
However I am trying to setup the same type of connection for another api which uses POST method.
The query looks something like.. URL + JSON API query which is similar to..
{"JSON" : {
"name": "Dataset",
"ColumnSelect": ["Name","Age","Email"],
"sort":["Name"],
"filterList": [
{
"Attribute": "Age",
"Operator": ">",
"Value": ["25"]
}],"returnObject"
I am not sure how this can be used to query the data and load data to sql like the http request. Can someone please advise me on the right direction to achieve this. Thank you for all your help.
I'm not %100 sure what your scenario is, but as far as I understand you are trying to get data from some server, and store the data in your database.
You have achieved it using HTTP GET, but this time you need to do it with POST.
The basic difference with GET and POST is, GET is used to query the existing data, but in POST, you are delivering something to the server. (of course there are more differences, check this link: https://www.w3schools.com/tags/ref_httpmethods.asp)
This can be done easily:
Prepare your POST request, you need to have a content for your parameters.
Create classes for your incoming response string, from your JSON posted:
public class JSON
{
public string name { get; set; }
public string[] ColumnSelect { get; set; }
public string[] sort { get; set; }
public filterList filterList { get; set; }
}
public class filterList
{
public string Attribute { get; set; }
public string Operator { get; set; }
public string[] Value { get; set; }
}
When you have the response string (stringified JSON), deserialize it via JSON.NET.
Now you have them as .NET objects. You can use Entity Framework to commit them to the database.
I can edit my answer if you need advice on any steps.
I am using swagger ui for the documentation of my ServiceStack web-services which works quite nice. However I did not find a way to add any information about the responses of those web-services (json). So when the GET method of my service looks like this:
public object Get(CustomerData customers)
{
List<CustomerDAO> customers = DataProvider.GetAllCustomers();
return customers;
}
public class CustomerDAO
{
// this is important information which needs to be visible in swagger-ui
public string Name { get; set; }
public string SomeImportantProperty { get; set; }
public string AnotherPropery { get; set; }
public string AlsoWorthDocumenting { get; set; }
// and so on...
}
I need a short description of each property in the documentation (swagger-ui) which is essential for using those web-services.
I am using ServiceStack 3.9.74 but this should also apply to the latest ServiceStack version. If not please let me know.
I was wondering how I can get this information in swagger, or if not, how to supply such important information to the developers who have to work with my web services. I am also open for any other approaches or best practices which will work in the environment of ServiceStack.
I'm not sure about your version of ServiceStack. In the latest version you would have your request dto inheirt IReturn<ReturnClassHere> which would tell swagger to look at that return class.
So your CustomerData class would look something like this:
public class CustomerData : IReturn<List<CustomerDAO>>
{
}
Although it would probably be better to setup another dto for your response that would be returned instead. Maybe something like CustomerDataResponse.
I've added an existing method to a web service (that I did not write).
I can bring the method into Soap UI by performing and update on the service.
I can run the method in SOAP UI and debug it and clearly see my method is pulling the data I want, processing it correctly but I am getting nothing back in Soap UI and my debugger terminates at that point.
Its worth nothing I build this method from an existing working method that returns data just fine. I imagine I am just missing some minor configuration some ware.
Things I have done,
1) Added the new method to the operation contract
2) Added all the necessary logic for processing the data.
I can furnish any code/configs but I just don't know what exactly is needed to troubleshoot this as I am attempting to add this off existing code. I'm generally not a vague with the details but I'm in the process of educating myself at the same time as well as trudging through some existing documentation I have found online.
The service is intended to return a class. We define a DTO and paste the results from a SQL query into our DTO. We then do some integrity checking on the DTO and test for failure or success. We then add the DTO to a container object named CustomResult based on success or failure and return the CustomResult. The CustomResult class should not need any modifying (famous last words). I did however create a new DTO class which I can add to my original post
DTO Class
namespace Custom.Company.Services
{
[DataContract]
public class TimeUnitDto
{
[DataMember]
public string Calendar { get; set; }
[DataMember]
public long AverageHour { get; set; }
[DataMember]
public long AverageDay { get; set; }
[DataMember]
public long AverageWeek { get; set; }
[DataMember]
public long AverageMonth { get; set; }
[DataMember]
public long AverageYear { get; set; }
[DataMember]
public long LookupRefreshInd { get; set; }
}
}
Thanks,
All, I figured it out. Unfortunately the answer is propriety to what I am doing but basically we the DTO to our CustomResult Class.
I copy/pasted the code from another service we use since the functionality was similar. I forgot to replace of the 'Status' assignments to the new status I created for this dto.
It was trying to place it in a Status object that the data is not going to recognize. Thanks to everyone who looked into this for me.
I have a working C# Web Service running on IIS on a dedicated server. There is a method that gets Session object(DataContract) and its tested and working on Windows clients. However now I'm developing a "Xamarin.Mac" application and I need to connect to server , but I'm facing a specific error.
[DataContract]
public class Session{
[DataMember]
public string Email { get; set; }
[DataMember]
public string Password { get; set; }
[DataMember]
public string ComputerHash { get; set; }
[DataMember]
public string Ip { get; set; }
}
This is the Session class I'm trying to send.
[OperationContract]
bool Login ( Session session );
And this is the method I'm calling.
But when I'm debugging, I checked that my Session object is filled and the values are set. But server gets the Session object but all fields of the object are set to null. Thus it gives me null exception.
Interestingly non-complex objects are being sent without any problem, but these complex types are giving me headaches.
What I might be missing here?
That depends on how you're making the request. You need to "post" the data to the server. The data needs to be defined in the body of the message, not as parameters in the request url.
Given an app that needs to exchange info such as ConsumerKey/ConsumerSecret for oAuth from
- server running "WebApi" over ServiceStack
- consumer is a desktop app
Looking to encrypt the messages using Public/Private keys which are already in place
Is there any support for encrypting/decrypting messages, hopefully transparently ? i.e. the encrypting/decryption is handled via say Filters on the server ?
OR
any workaround on how to do this ?
FYI - the server already requires SSL and requests are authenticated, however the registration is free/automated, hence the need for encryption
Update
The Encrypted Messaging Feature released in v4.0.42 now enables a secure channel for all Services offering protection to clients who can now easily send and receive encrypted messages over unsecured HTTP.
There's no published support for Message Level encryption built into the ServiceStack at the moment, although this is something we're thinking of adding in the future. Below is an example of an approach you could try with ServiceStack today for exploratory purposes (it will require more R&D on your part to come up with a good solid design), and as this is an area that may change in future, it's not a use-case we're supporting right now, but here's something that could work with ServiceStack as it is now:
class EncryptedRequest
{
//Name of the request type
public string Type { get; set; }
//Serialized Request DTO using something like JSON
public string EncryptedBody { get; set; }
//optional: let server the private key that was used (if multiple)
public string PrivateKeyMd5Hash { get; set; }
}
class EncryptedResponse
{
//Name of the response type
public string Type { get; set; }
//Serialized Response DTO
public string EncryptedBody { get; set; }
//optional
public string PublicKeyMd5Hash { get; set; }
}
Here is a convenient CryptUtils wrapper that makes it easy to create Public/Private keys as well as being able to Encrypt/Decrypt text using them.
Then you can have a single service, and do something like:
class EncryptedService : Service
{
const string PublicKey = ...;
const string PrivateKey = ...;
EncryptedResponse Any(EncryptedRequest request)
{
var requestType = Type.GetType("{0}.{1}"
.Fmt(typeof(EncryptedRequest).Namespace, request.Type));
var json = CryptUtils.Decrypt(PrivateKey, request.EncryptedBody);
var requestDto = JsonSerializer.DeserializeFromString(json,requestType);
var responseDto = GetAppHost().Config.ServiceController
.Execute(requestDto, base.RequestContext);
return new EncryptedResponse {
Type = responseDto.GetType().Name,
EncryptedBody = CryptUtils.Encrypt(PublicKey, responseDto.ToJson()),
};
}
}
The client would also need a copy of the Public/Private key pair which you would exchange out-of-band.