Direct call User defined function in Entity Framework 5 - c#

I have in model.store my user defined function, but i can't import in "Function Import".
I tryed to use this syntax:
public class dbFunction
{
[EdmFunction("MyModel.Store", "FN_GETPARVALUE")]
public static string FN_GETPARVALUE(decimal ditta, string par, string retIfNull)
{
throw new NotSupportedException("Direct calls are not supported.");
}
}
but I can't call directly like this:
CommonRepository pr = new CommonRepository();
var test = pr.parametriDittaGetValue(Near_UserDitta, "PRODGESCOL", "");
How can I resolve my Problem?
Thanks

Related

Load multiple bindings with Ninject

I am trying to load bindings in a class for a project. I am using 3rd party extensions for Caching and the class I need to load looks like below using c# and .net framework 472 .
public class CouchbaseCache : ICouchbaseCache, IDistributedCache
{
public CouchbaseCache(ICouchbaseCacheBucketProvider provider, IOptions<CouchbaseCacheOptions> options);
public IBucket Bucket { get; }
public CouchbaseCacheOptions Options { get; }
}
usually, If I have to load, I would use something like
Bind().To().InSingletonScope();
But how would I do it for the above class by giving the bucket info and Options as values while loading it? I could not get my head around it.
Also, ICouchbaseCachebucketProvider is an interface derived from INamedbucketProvider and derived class looks like
public interface INamedBucketProvider
{
string BucketName { get; }
IBucket GetBucket();
}
So far, I was able to get CouchbaseClientDefinition set up like this
Bind<ICouchbaseClientDefinition>().ToMethod(ctx =>
{
var options = new CouchbaseClientDefinition
{
Servers = new List<Uri>
{
new Uri("http://couchbase.com/")
}
};
return options;
}).InSingletonScope();
I need to give Uri for couchbase and also bucket name and the logic is all over the place. Any knowledge sharing will be greatly appreciated.
if the argument for the constructor of the CouchbaseCache is identical for the whole application life time then you can bind it with the use of binding with constructor arguments something like this where you are loading:
var options = new CouchbaseClientDefinition
{
Servers = new List<Uri>
{
new Uri("http://couchbase.com/")
}
};
var couchbaseCacheBucketProvider= new CouchbaseCacheBucketProvider
{
...
};
Bind<ICouchbaseClientDefinition().To<CouchbaseCache >()
.WithConstructorArgument(couchbaseCacheBucketProvider, options);
but you have to provide the couchbaseCacheBucketProvider.
if the arguments are different but they are limited for example if you have two version of the arguments you can use the named binding like this
var options1 = new CouchbaseClientDefinition
{
...
};
var options2 = new CouchbaseClientDefinition
{
...
};
var couchbaseCacheBucketProvider1= new CouchbaseCacheBucketProvider
{
...
};
var couchbaseCacheBucketProvider2= new CouchbaseCacheBucketProvider
{
...
};
Bind < ICouchbaseClientDefinition().To<CouchbaseCache>().WithConstructorArgument(couchbaseCacheBucketProvider, options1).Named("FirstBinding");
Bind < ICouchbaseClientDefinition().To<CouchbaseCache>().WithConstructorArgument(couchbaseCacheBucketProvider, options2).Named("SecondBinding");
However another Alternative is to use the FactoryPattern/Singleton to create your CouchbaseCache object. Then you need just to inject the Factory Class that you created and you can use the Factory Class to get the required CouchbaseCache object whenever it is required.

How to write unit test for this block of code

public static LoginResult CreateLoginSuccessResponse(this PortalIdentity user)
{
return new LoginResult(
true,
string.Empty,
user.Roles,
false
);
}
public class PortalIdentity : IdentityUser
{
public string Firstname { get; set; }
public string Lastname { get; set; }
}
This block of code is for creating a success response whenever a user logs in successfully. I am a QA and trying to learn how to write unit tests. I am not sure how to write unit test for "this" keyword
this (in the above context at least) just means it is an extension method.
To test it, new up a PortalIdentity, assign to a variable (bob) and call bob.CreateLoginSuccessResponse():
var bob = new PortalIdentity(you may need parameters here);
var result = bob.CreateLoginSuccessResponse();
If the call to bob.CreateLoginSuccessResponse() doesn't compile, then the extension method is likely in a different namespace. As per the docs, you need to:
In the calling code, add a using directive to specify the namespace
that contains the extension method class.

C# How to get/set data from asmx Web Service

I'm using VisualStudio2013. Its important to note for readers that the code which this asmx is derived from works perfectly but I do not know how to use the asmx WebService. I downloaded the whole nine yards from here https://sourceforge.net/projects/shorturl-dotnet/
I cannot figure out how to get/set properties of the following CreateUrl() WebMethod. I want to learn how to use the entire WebService but started here.
In the example that follows I send a URL to the CreateURL() method which will shorten the URL and perform other tasks; I do not know how to get properties from the returned ShortUrl.Container Class: I have not been successful accessing the data after the class(es) are returned to my calling method.
// WebMethod
public class API : System.Web.Services.WebService {
[WebMethod]
public ShortUrl.Container CreateUrl(string real_url)
{
ShortUrl.Container oShortUrl = new ShortUrl.Container();
oShortUrl.RealUrl = real_url;
oShortUrl.ShortenedUrl = ShortUrl.Utils.UniqueShortUrl();
oShortUrl.CreateDate = DateTime.Now;
oShortUrl.CreatedBy = HttpContext.Current.Request.UserHostAddress;
ShortUrl.Utils.AddUrlToDatabase(oShortUrl);
oShortUrl.ShortenedUrl = ShortUrl.Utils.PublicShortUrl(oShortUrl.ShortenedUrl);
return oShortUrl;
}
}
// ShortUrl.Container class returned as oShortUrl
namespace ShortUrl
{
/// <summary>
/// Container for the ShortURL object
/// </summary>
public class Container
{
private string _real_url;
private string _short_url;
private DateTime _create_date;
private string _created_by;
public Container()
{
this.CreateDate = DateTime.Now;
this.CreatedBy = "tap";
this.RealUrl = null;
this.ShortenedUrl = "Unknown";
}
public string RealUrl
{
get { return _real_url; }
set { _real_url = value; }
}
public string ShortenedUrl
{
get { return _short_url; }
set { _short_url = value; }
}
public DateTime CreateDate
{
get { return _create_date; }
set { _create_date = value; }
}
public string CreatedBy
{
get { return _created_by; }
set { _created_by = value; }
}
}
}
In VS2013 I add the Service Reference to point to http://tap.tools.api.asmx as the service endpoint and name the VS2013 reference as ShortenUrl. VS2013 generates the APISoapClient and Container classes.
// get/set properties of the ShortUrl.Container class
// by means of APISoapClient
ShortenUrl.APISoapClient u = new ShortenUrl.APISoapClient();
u.CreateUrl("http://clintongallagher.com/tag-target-url.html");
// get/set properties of the ShortUrl.Container class
// by means of Container class
ShortenUrl.Container c = new ShortenUrl.Container();
string url = c.RealUrl;
I'm not getting anywhere with either and I think my problem is the instance of the oShortUrl object instantiated within the public ShortUrl.Container CreateUrl(string real_url) method. I do not know how to get any of the properties from that instance of oShortUrl the Container class returns to my methods.
// oShortUrl
ShortUrl.Container oShortUrl = new ShortUrl.Container();
Odd as it may sound as old and outdated the use of asmx happens to be I never worked with -any- WebServices yet which explains why I am weak and throw myself to the mercy of the court.
// EDIT: 2016-07-19 ~2:41pm
VS2013 generated several classes from the WSDL two of which appear to be useful as seen in Intellisense...
// class APISoapClient and class Container
When I use a local variable with APISoapClient a shortened URL is generated as I can see using SQL Management Studio and note all of the data is properly generated but I am not able to get/set on any other WebMethods or properties with to get/set data...
// Exposes two WebMethods: CreateUrl and GetUrl
ShortenUrl.APISoapClient u = new ShortenUrl.APISoapClient();
// Does generate the shortened URL
u.CreateUrl("http://clintongallagher.com/tag-target-url.html");
// Should return the URL that was shortened but doesn't
u.GetUrl("i2Z5H");
And...
// Exposes the properties in Intellisense but does not return data
ShortenUrl.Container c = new ShortenUrl.Container();
// returns 1/1/0001 12:00:00 AM
lblCreateDate.Text = "CreateDate: " + c.CreateDate.ToString();
// returns nothing
lblCreatedBy.Text = "CreatedBy: " + c.CreatedBy;
// returns nothing
lblRealUrl.Text = "RealUrl: " + c.RealUrl;
// returns ShortenUrl.Container
lblShortenedUrl.Text = "ShortenedUrl: " + u.GetUrl("i2Z5H");
If i understood what you're trying to get is the Container returned from the Web Method. If so then just create a variable type of Container and assign the method call to it. Like ShortUrl.Container c = u.CreateUrl(...) then from c you can get the values you're looking for.
Think about this #clintongallagher. When you do the following call,
ShortenUrl.APISoapClient u = new ShortenUrl.APISoapClient();
u.CreateUrl("http://clintongallagher.com/tag-target-url.html");
[WebMethod]
public ShortUrl.Container CreateUrl(string real_url)
{
ShortUrl.Container oShortUrl = new ShortUrl.Container();
oShortUrl.RealUrl = real_url;
//here you're assigning a value to this object, let's say 'A'
oShortUrl.ShortenedUrl = ShortUrl.Utils.UniqueShortUrl();
oShortUrl.CreateDate = DateTime.Now;
oShortUrl.CreatedBy = HttpContext.Current.Request.UserHostAddress;
//then here you're saving the object with the Shortened value 'A' you just got
ShortUrl.Utils.AddUrlToDatabase(oShortUrl);
/*
*finally you're replacing the Shortened value with another value,
*let's say 'B', which is the object you're going to return*/
oShortUrl.ShortenedUrl = ShortUrl.Utils.PublicShortUrl(oShortUrl.ShortenedUrl);
return oShortUrl;
}
I don't know how does GetUrl(shortened_value) is supposed to work but, assuming it will get from the DB the shortened_value passed in, of course the result would not be the same since the shortened value saved was 'A' and your asking for B.

How to write a C# unit test in Visual Studio?

This is my first unit test and wanted some help clearing out my thoughts about the process of writing a unit test.
I wanted to write a test method that will add a new user - using my AddUser method in my library class.
Document doc = new Document();
[TestMethod]
public string AddUser()
{
string name = doc.AddUser("Testing User");
Assert.IsNotNull(name);
}
The error I am getting on build:
Cannot implicitly convert type void to string
This is my AddUser method:
public void AddUser(string newUserName)
{
using (var db = new DataContext())
{
User user = new User()
{
FullName = newUserName,
ID = Guid.NewGuid()
};
db.Users.InsertOnSubmit(user);
db.SubmitChanges();
}
}
Your method does not have a return value:
public void AddUser
^^^^ no return value
So you can't store it into a string:
string name = doc.AddUser("Testing User");
^^^^^^^^^^^ AddUser has no return value
Make sure you return the name from from your AddUser(string newUserName) method.
Replace your method like
public String AddUser(string newUserName)
{
using (var db = new DataContext())
{
User user = new User()
{
FullName = newUserName,
ID = Guid.NewGuid()
};
db.Users.InsertOnSubmit(user);
db.SubmitChanges();
}
return newUserName;
}
AddUser doesn't return anything.
AddUser is written as a method, not a class. Your test attempts to call it like a method, but the method is void, which does not return a value.
Simply speaking you can't assign void to string.
To make it work you can do in 2 ways:
make your method AddUser return string type or add extra parameter to your method and use keyword out for this parameter
hope it help.

Can I Create a Web Service that has Properties?

I'm trying to test code around a web service that is not available yet. I'm trying to dummy up my own version. According to the specs it will be called like this.
var service = new Service();
service.SD = new ServiceData();
service.SD.ID = "ABC123";
service.SD.Auth = "00000";
string result = service.DoMyThing();
This is the closest I've gotten.
var service = new Service();
service.set_SD(new ServiceData());
service.get_SD().ID = "ABC123";
service.get_SD().Auth = "00000";
service.DoMyThing();
The problem is with the SD property. How do I write the service so that Visual Studio 2008 generates the web reference correctly?
Here is my current dummy web service code.
public class Service : System.Web.Services.WebService
{
// This doesn't show up in the generated proxy at all
public static ServiceData SDTest;
// For extra credit explain why this needs to be static for it to work
private static ServiceData _sd;
public ServiceData SD
{
[WebMethod(EnableSession = true)]
get { return _sd; }
[WebMethod(EnableSession = true)]
set { _sd = value; }
}
[WebMethod]
public string DoMyThing()
{
// Presumably the real service accesses SD in here
return "";
}
}
public class ServiceData
{
public string ID { get; set; }
public string Auth { get; set; }
}
Your design is flawed. Web services are not meant to have properties. They should only expose methods, the reason being that the HTTP protocol is stateless (and web services assume this too), so exposing a property doesn't make sense unless you want it to apply to all callers of the instance (and still, even in that situation, it doesn't make sense to expose it).
Rather, what you want to do is have the DoMyThing method take the instance of ServiceData (if required) and operate on that, returning the appropriate result set.
If you really have a need to expose properties of the service, you would have a GetProperties method (or something like that) which takes no parameters and returns the appropriate data structure with the service information.
I'm with casperOne on this. I think using fakie properties are more annoying than useful.
Still, if you're married to this just eliminate the getter for the property. You don't need it. Do this instead:
var service = new Service();
ServiceData sd = new ServiceData();
sd.ID = "ABC123";
sd.Auth = "00000";
service.SD = sd;
string result = service.DoMyThing();
If Visual Studio still names the setter property incorrectly you can use one of the soap attributes to rename it.
EDIT: You'll also need to define SD as a SOAP Header.
You can't do this, so don't try to "fake it". The best you can do is:
var service = new Service();
ServiceData sd = new ServiceData();
sd.ID = "ABC123";
sd.Auth = "00000";
string result = service.DoMyThing(sd);
For those that may be interested.
This more accurately reflects the spec than my sanitized version above (I didn't know "TypeNameValue" was a key clue, sorry!).
var service = new Service();
service.ServiceDetailsValue = new ServiceDetails();
service.ServiceDetailsValue.ID = "ABC123";
service.ServiceDetailsValue.Auth = "00000";
string result = service.DoMyThing();
And this is the dummy web service code that works.
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(Name="TestService", ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public ServiceDetails SDTest;
[WebMethod]
[SoapDocumentMethod(Binding = "TestService")]
[SoapHeader("SDTest", Required = true)]
public string DoMyThing()
{
return "";
}
}
public class ServiceDetails : System.Web.Services.Protocols.SoapHeader
{
public string ID { get; set; }
public string Auth { get; set; }
}

Categories