I'd like to use a web service from a database to gather informations. Right now, I implemented to web service, turned it into a proxy class via wsdl.exe but I'm slightly irritated by the outcome. The normal way to call that class is new object -> method -> parameters ->happiness. This thing only consists of partial classes and wants strange parameters. I'm not even sure if I got the right method to get the wanted information.
This seems to be the needed method:
public UniProtId2DomainIdsRecordType[] UniProtId2DomainIds (UniProtId2DomainIdsRequestRecordType UniProtId2DomainIdsRequestRecord)
{
object[] results = this.Invoke("UniProtId2DomainIds", new object[] {
UniProtId2DomainIdsRequestRecord});
return ((UniProtId2DomainIdsRecordType[])(results[0]));
}
This seems to be one of the needed classes:
public partial class UniProtId2DomainIdsRequestRecordType
{
private string uniprot_accField;
/// <remarks/>
public string uniprot_acc
{
get
{
return this.uniprot_accField;
}
set
{
this.uniprot_accField = value;
}
}
}
(That's the whole class, generated by wsdl.exe -> https://www.dropbox.com/s/yg909ibdq02js5a/GetCath.cs)
But as soon as I try to use it as I think it should work... well... my experiments on this (none of them working):
UniProtId2DomainIdsRequestRecordType Uni2Cath = new UniProtId2DomainIdsRequestRecordType();
Uni2Cath.uniprot_acc = "P0A7N9";
UniProtId2DomainIdsRecordType[] UniProtId2DomainIds;
UniProtId2DomainIdsRecordType test = new UniProtId2DomainIdsRecordType();
test.uniprot_acc = "P0A7N9";
UniProtId2DomainIdsRecordType[] UniProtId2DomainIds(test);
All I need is to get a string like P0A7N9 to be passed to the server.
(The reference to this webservice: http://api.cathdb.info/api/soap/dataservices/wsdl#op.o159501052 )
Can someone give me a hint how to handle this, please?
The easiest way would be to add this web service as Service Reference to your project. Then you can call the different methods. Use this as the address: http://api.cathdb.info/api/soap/dataservices/wsdl
using (var ser = new DataServicesPortTypeClient())
{
var results = ser.UniProtId2DomainIds(new UniProtId2DomainIdsRequestRecordType
{
uniprot_acc = "P0A7N9"
});
if (results != null)
{
var geneName = results.gene_name;
var speciesName = results.species_name;
}
}
If you want to use your generated class do this:
using (var service = new DataServices())
{
var results = service.UniProtId2DomainIds(new UniProtId2DomainIdsRequestRecordType
{
uniprot_acc = "P0A7N9"
});
if (results != null && results.Length >0)
{
var geneName = results[0].gene_name;
var speciesName = results[0].species_name;
}
}
As John suggested in the comments, ASMX and wsdl.exe are deprecated technologies. You should be using Service References and svcutil.exe
Related
I have read almost all the related questions, but unfortunately no one solved my problem.
My problem is that I have created a C# web service and created four methods into it for different purposes and one of them called getSections here it is:
[WebMethod]
public string getSections()
{
connectToDB();
da = new SqlDataAdapter("select * from Sections", link);
dt = new DataTable();
da.Fill(dt);
return Newtonsoft.Json.JsonConvert.SerializeObject(dt,Newtonsoft.Json.Formatting.Indented);
}
when I test the web service from the visual studio it shows links to the four methods to invoke them one by one, all of them work fine, so I uploaded them to my server. After that I tried to access to getSections method from my app using volley and here how I have done:
private fun getAllSections() {
/**
* Start Downloading all the sections
*/
var uri="http://......../myProject/WebService1.asmx?op=getSections";
val stringRequest = object : StringRequest(Request.Method.POST, uri, Listener<String> { response ->
Log.i("SectionsResponse", response)
Toast.makeText(this,"SectionsResponse: "+response,Toast.LENGTH_LONG).show()
}, object : Response.ErrorListener {
override fun onErrorResponse(error: VolleyError?) {
if (error != null) {
Log.i("SectionsError:", error.message.toString())
Toast.makeText(baseContext,"error"+error.message.toString(),Toast.LENGTH_LONG).show()
}
}
}) {
override fun getParams(): MutableMap<String, String> {
val params=HashMap<String,String>()
//params.put("a","aa")
return params
}
}
if (Funs().isOnline(this)) {
val requesQueue = Volley.newRequestQueue(this)
requesQueue.add<String>(stringRequest)
} else {
Toast.makeText(this, resources.getString(R.string.connectionFailed), Toast.LENGTH_LONG).show()
}
/**
* End Downloading all the sections
*/
}
After all above I am getting a null result from the error listener which means that the url is not valid or not found.
so
what is the valid way to access to the method into c# web service using volley library.
appreciate any help.
I am trying to get Nancy to load routes based on data on disk (I read dirs and make routes based on those):
public class StaticModule : NancyModule {
public static string[] RegisteredApps;
public StaticModule() {
foreach (string app in RegisteredApps) {
Get[app + "{file*}"] = parameters => SendFile(app, parameters);
Get[app] = parameters => SendFile(app, parameters);
}
}
}
Then in my calling code, I initialise the statics:
StaticModule.RegisteredApps = { "test1", "test2" };
NancyHost h = new NancyHost();
h.Start();
By using breakpoints, I can see the StaticModule is initialised during the call to new NancyHost(). I reckon this is where the route cache is built.
And indeed, using the Nancy interactive diagnostics, I can see the routes. So far so good.
However, when I try to access one of the routes, I get a 404, without the StaticModule's constructor even called.
Where does this go wrong?
Are you sure it's failing? I've just run same here and it works just fine - though I think you may well be meaning to change
Get[app + "{file*}"] = ...
To
Get[app + "/{file*}"] = ...
In a generated Service Reference (imported from a WSDL), I have the following methods in the Client class, in the Reference.cs:
public Namespace.Service.SalesOrderDetail newService(Namespace.Service.Contact orderContact, Namespace.Service.Contact installationContact, string customerReference, Namespace.Service.ServiceDetails[] serviceDetailsList) {
Namespace.Service.newServiceRequest inValue = new Namespace.Service.newServiceRequest();
inValue.orderContact = orderContact;
inValue.installationContact = installationContact;
inValue.customerReference = customerReference;
inValue.serviceDetailsList = serviceDetailsList;
Namespace.Service.newServiceResponse retVal = ((Namespace.Service.ServiceRequestPortType)(this)).newService(inValue);
return retVal.salesOrder;
}
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
System.Threading.Tasks.Task<Namespace.Service.newServiceResponse> Namespace.Service.ServiceRequestPortType.newServiceAsync(Namespace.Service.newServiceRequest request) {
return base.Channel.newServiceAsync(request);
}
public System.Threading.Tasks.Task<Namespace.Service.newServiceResponse> newServiceAsync(Namespace.Service.Contact orderContact, Namespace.Service.Contact installationContact, string customerReference, Namespace.Service.ServiceDetails[] serviceDetailsList) {
Namespace.Service.newServiceRequest inValue = new Namespace.Service.newServiceRequest();
inValue.orderContact = orderContact;
inValue.installationContact = installationContact;
inValue.customerReference = customerReference;
inValue.serviceDetailsList = serviceDetailsList;
return ((Namespace.Service.ServiceRequestPortType)(this)).newServiceAsync(inValue);
}
I've seen Python code that uses the same WSDL, and it is able to access the method as response = client.newService(request).
I'd also like to access the method in that fashion, albeit var task = client.newService(request); Task.WaitAll(task); var response = task.Result;, but I can't seem to find the right combo of creating the service reference, without being forced to have expanded input parameters to the service.
Is there a magic combo for Service Reference creation that will allow me to just pass the request as a single object?
I'm not fussed on keeping the async functionality.
The client of a service implements the interface that represents the service. It just so happens, and is shown in this example, that it doesn't necessarily make all those implemented method public.
So, to get around this, if I cast the client object to the service interface, I get to call the service as intended, regardless of what the client has made public.
var client = new ServiceClient();
var service = (Service)client;
var request = new newServiceRequest() { ... };
var response = service.newService(request);
client.Close();
Below is my Intializer.cs and I was told in order to keep my Guids i had to use Navigation properties so that i had the right relations in my database(Reusing a GUID in EF Code First DatabaseIntializer). That seems to solves the issues i had earlier but now that i want to take my information and use a Seed to actually add it to the database, i am not sure how to satisfy this error. I get the error for addUsers(Applications apps)"eflogin.Models.Applications is a 'type' being used like a variable." I got the feeling i am doing this way wrong.
public class DatabaseIntializer : DropCreateDatabaseIfModelChanges<DataContext>
{
protected override void Seed(DataContext context)
{
addApplications().ForEach(a => context.Applications.Add(a));
addUsers(Applications apps).ForEach(u => context.User.Add(u));
// if i take out Applications apps
// i get No overload for method"addUsers" takes 0 arguments
}
private static List<Applications> addApplications()
{
var apps = new List<Applications>
{
new Applications
{
ApplicationId = Guid.NewGuid(),
ApplicationName = "Test Login"
}
};
return apps;
}
private static List<Users> addUsers(Applications apps)
{
var use = new List<Users>
{
new Users
{
UserId = Guid.NewGuid(),
UserApplication = apps,
UserName = "Ralph",
IsAnonymouse = false,
LastActivityDate = System.DateTime.Now
}
};
return use;
}
The problem is your are passing in the type and instance in the call to the addUsers method.
addUsers(Applications apps)
If you remove Applications and just leave apps like so.
addUsers(apps)
You will get another error because you are passing in a collection of objects and the method expects a single instance.
Here is a suggested edit to your Seed method that should get you past both errors.
var apps = addApplications();
apps.ForEach(a => context.Applications.Add(a));
foreach (var app in apps)
{
var users = addUsers(app)
users.ForEach(u => context.User.Add(u));
}
Note: I think keeping the entity names plural helps in causing some confusion.
OK, I am mostly a LAMP developer so I am new to the entity framework. However, I am familiar with the basics in LINQ and have generated a entity model from my DB. Now here is my requirement:
I have a datagrid on a WinForm that will be refreshed from a data source on a remote server every few seconds as changes to the dataset are made from other sources. Obviously, I'd like to construct a lambda expression to get the right anonymous type to satisfy the columns that needs to be shown in my datagrid. I have done this and here is the result (I'm using a custom datagrid control, btw):
And my code thus far:
Models.dataEntities objDB = new Models.dataEntities();
var vans = from v in objDB.vans
select v;
gcVans.DataSource = vans;
OK, so now I have my basic data set. One problem I had is that the "Status" column will show a calculated string based on several parameters in the data set. I added this to my entities via a partial class. As you can see in the screenshot, this is working correctly:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1.Models {
public partial class van {
public string van_status {
get {
if (this.is_offline == 1) {
return "Offline";
} else if (this.is_prayer_room == 1) {
return "In Prayer Room";
} else {
return "TODO: Create statuses";
}
}
}
}
}
This added property works fine. However, the second I try to project the status in an anonymous type so I can also retrieve the school name, I get an error:
Models.dataEntities objDB = new Models.dataEntities();
var vans = from v in objDB.vans
select new {
van_name = v.van_name,
school_name = v.school.school_name,
capacity = v.capacity,
phone = v.phone,
van_status = v.van_status
};
gcVans.DataSource = vans;
So, I have two questions:
1) If I cannot use computed properties of my partial classes in LINQ projections, how am I supposed to show my computed string in my datagrid?
2) Am I even approaching this correctly? When I resolve #1, how would I refresh this data (ie during a timer fire event)? Would I simply call objDB.refresh() and then tell my datagrid to update from the datasource? Does calling that method actually run the lambda expression above or does it load everything from the DB?
Thanks for any assistance with this. Also, if you have any best practices to share that would be awesome! I hope I explained this as thoroughly as you need to provide assistance.
1) Instead of modifying your EF object with a partial class you could always create your own class that contains your read only property van_status. The code you've got would be nearly identical:
Models.dataEntities objDB = new Models.dataEntities();
gcVans.DataSource = from v in objDB.vans
select new DisplayVan {
van_name = v.van_name,
school_name = v.school.school_name,
capacity = v.capacity,
phone = v.phone,
};
The van_status property, since it's read-only, will not need to be specified in the query.
2) I'm more a web developer than a desktop developer so I'll give you my take on how to refresh the grid (it may not be the preferred methodology for fat clients)...
I'm reluctant to trust .Refresh() methods and hope all works to maximal efficiency and work properly. Instead, encapsulate the code from #1 in a method of your own and invoke it from your timer event firing (or however you choose to implement the periodic refresh).
Another good option would be to create an extension method.
Here is a simple example:
using System;
namespace WindowsFormsApplication1 {
static class Program {
[STAThread]
static void Main() {
Van van = new Van();
string status = van.GetStatus();
}
}
public static class VanExtension {
public static string GetStatus(this Van van) {
if(van.is_offline == 1) {
return "Offline";
}
else if(van.is_prayer_room == 1) {
return "In Prayer Room";
}
return "TODO: Create statuses";
}
}
public class Van {
public int is_offline { get; set; }
public int is_prayer_room { get; set; }
}
}
Be sure to put this extension class in the same namespace as the entity class.