MicroOrm.Dapper.Repositories SqlGenerator param - where to get that? - dapper

Im trying to use the MicroOrm.Dapper.Repositories but the usage sample left me hanging on where to get the SqlGenerator object. It's sample documentation is:
public UserRepository(IDbConnection connection, ISqlGenerator<User> sqlGenerator)
: base(connection, sqlGenerator)
{
Amy I using this right:
var conn = new MySqlConnection("connectionStringXYZ");
var generator = new SqlGenerator<User>();
var userRepo = new UserRepository(conn, generator);
which gives me internal server error 500.

You should be able to do:
ISqlGenerator<User> userSqlGenerator = new SqlGenerator<User>(ESqlConnector.MySQL);
Preferably the generator should be a singleton, possibly an injected one.
But your error is an http error, so I suppose you are swallowing the real exception somewhere in your application. Find out the underlying reason.

Related

Trouble Getting HttpResponse data with AngularJS $http.post

I am try to return a custom object from VB.NET code behind to a AngularJS HTTP Post. Based on the user requests, this object could be very large. Very, very large. Whenever that happened, I got a OutOfMemoryException when trying to serialize it. Therefore, I am now using the JSON.NET package from Newtonsoft to serialze it using a StringWriter:
Using sw As New StringWriter()
Using writer As JsonWriter = New JsonTextWriter(sw)
Dim serializer As New JsonSerializer()
serializer.Serialize(writer, periodData)
writer.Flush()
End Using
End Using
While this works, when I then try to do sw.ToString I still get that OutOfMemoryException.
So, I trawled through the internet and I believe I can use a StreamWriter sent to a HttpResponse object and then flush the response to return it to the web client call. However, I cannot figure out how to do this.
Any help? Or if I am way off track, give me a better way to do this?
Update 1
Apparently, I cannot use HttpResponse in a Using statement because I get a "using operand must implement system.idisposable" error. So now I am having trouble creating an HttpResponse object in the WebMethod. Passing both TextWriter and HttpWriter as params for the constructor give me errors.
Update 2
Okay, so I am no longer getting an OutOfMemoryException when trying to serialize the object. However, I believe I am doing something wrong since my StreamWriter doesn't seem to be writing to the HttpResponse Object. This is my current code:
Dim response As HttpResponse = HttpContext.Current.Response()
response.ContentType = "application/json"
response.Clear()
response.BufferOutput = True
Using sw As New StreamWriter(response.OutputStream, System.Text.Encoding.UTF8)
Using writer As JsonWriter = New JsonTextWriter(sw)
Dim serializer As New JsonSerializer()
serializer.Serialize(writer, periodData)
writer.Flush()
End Using
sw.Flush()
End Using
response.Flush()
Then when I pause my JavaScript in the Chrome developer console, the response object is just an empty string. The javascript is just a basic Angular $http.post call:
$http.post('url.aspx/GetData', angular.toJson({
param1: data1,
param2: data2,
param3: data3
})).then(function (response) {
//Do stuff with response which is currently an empty string.
}
I don't believe my VB.NET code is correct. Help?
It's not quite clear from your question where you are, but it looks like you are having trouble constructing a StreamWriter to write into a HttpResponse.
You can get the response object from the HttpContext:
[WebMethod]
public static void YourServiceMethod()
{
HttpResponse response = HttpContext.Current.Response;
// ... now go on
}
The response object has a property OutputStream that you can give to the constructor of StreamWriter (in a using statement), like in this answer to another question:
using (TextWriter textWriter = new StreamWriter(response.OutputStream, System.Text.Encoding.UTF8))
{
// ... now write your data
}
(Excuse me giving C# samples, that's what I am much more familiar with.)

Making JAX RS CXF Interceptors work with WebClient

I am having a problem making interceptors fire when using the WebClient class (org.apache.cxf.jaxrs.client.WebClient). In my method that calls a RESTful service I have added an interceptor to execute in the out phase. I have intentionally provided invalid properties so I can see the interceptor fail, but the method completes successfully.
Here is the code that I am working with:
private String callService2(String webServiceUrl) {
JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
bean.setAddress(webServiceUrl);
// setup properties
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("ws-security.signature.username", "client");
properties.put("ws-security.signature.properties",
"client_nonexistantfile.properties");
bean.setProperties(properties);
XmlSigOutInterceptor sigInterceptor = new XmlSigOutInterceptor();
bean.getOutInterceptors().add(sigInterceptor);
// use WebClient (or proxy) as usual
WebClient wc = bean.createWebClient();
TestInfoResponse response = wc.accept("application/xml").get(TestInfoResponse.class);
return response.getContents();
}
I am expecting the XmlSigOutInterceptor logic to fail because the properties file does not exist, but the method completes successfully. What am I doing wrong when adding the XmlSigOutInterceptor.
Thanks in advance.
This is my bad. the XmlSigOutInterceptor does not need to do anything when a get is performed because there is no document to sign. So the interceptor was firing, it just returned right away.
Sorry for the noise.

GWT RequestFactory not firing properly after using edit()

I have a problem using "fire()" with a GWT RequestFactory after I've used it to unfreeze and edit a proxy.
If I have two request factory objects and their associated contexts like this:
private SyntheticRequest req1 = requestFactory.someRequest();
private Request<xProxy> sendRequest1 = req1.something();
private SyntheticRequest req2 = requestFactory.someRequest();
private Request<xProxy> sendRequest2 = req2.something();
using "fire()" on the first request works fine:
sendRequest1.fire(new Receiver<xProxy>() {
#Override
public void onSuccess(xProxy response) {
...
if (somethingIsTrue){
xProxy x = req2.edit(response); //<-- **I think this causes a problem later, although the proxy "x" works as expected here.**
x.setSomething("something");
update();
}
});
that part runs ok because I get to the "onSuccess". But when this one runs "update()", which looks like this:
private void update(){
sendRequest2.fire(new Receiver<xProxy>(){
...onFailure...
...onSuccess...
});
}
sendRequest2 always fails, with the error
Server Error Index:0 Size:0
and I put a breakpoint in the code for the "something()" service and it never even gets to that code! There must be something about the "req2.edit()" that hurts req2 and sendRequest2, but what?
Thanks.
what is 'b'? the line xProxy x = req2.edit(b); is the first time it's mentioned? is it supposed to be xProxy x = req2.edit(response);
Anyway.. that is not the problem..
'Server Error' indicates that RequestFactory caught an exception during the processing of a request, server-side. Something (but maybe not something()) is throwing an IndexOutOfBounds exception.
If you have a look at RequestFactoryServlet.java (which you can replace with your own very easily btw) you can see it setting up a try catch block that catches all exceptions when processing a request. It passes them to 'DefaultExceptionHandler' which wraps them in a ServerFailure, and that gets returned to you GWT code as an onFailure() call.
An easy way to find where the exception is being thrown is set a breakpoint on IndexOutOfBoundsException, making sure to catch 'caught' exceptions as well as uncaught.

Using BlobRequest.CopyFrom fails with 404 Not Found error

Hope you can help.
I'm trying to copy a blob using the Protocol namespace along with a shared access signature, but the WebResponse always throws a 404 Not Found error. I have successfully used the Get/Post/Delete/List methods (where the 404 would be thrown if the permissions were insufficient), but I cannot find the answer here.
Here's some simple code that I am using:
Uri uriFrom = new Uri("file://mymachine/myfile.txt");
Uri uriTo = new Uri("file://mymachine/myfile1.txt");
//get shared access signature - set all permissions for now
uriTo = GetSharedAccessSignature(uriTo, SharedAccessPermissions.Write |
SharedAccessPermissions.Read | SharedAccessPermissions.List);
//NOTE: This returns my uriTo object in the following format:
//http://mystoragespace.blob.core.windows.net/mycontainer/steve1.txt?se=2011-07-04T12:17:18Z&sr=b&sp=rwdl&sig=sxhGBkbDJpe9qn5d9AB7/d2LK1aun/2s5Bq8LAy8mis=
//get the account name
string accountName = uriTo.Host.Replace(".blob.core.windows.net", string.Empty);
//build the canonical string
StringBuilder canonicalName = new StringBuilder();
canonicalName.AppendFormat(System.Globalization.CultureInfo.InvariantCulture,
"/{0}/mycontainer{1}", accountName, uriFrom.AbsolutePath);
//NOTE: my canonical string is now "/mystoragespace/mycontainer/myfile.txt"
//get the request
var request = BlobRequest.CopyFrom(uriTo, 300, canonicalName.ToString(),
null, ConditionHeaderKind.None, null, null);
request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
//perform the copy operation
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
//do nothing. the file has been copied
}
So, my uriTo seems to have the appropriate permissions (I've tried various combinations) and the canonical string seems to have the correct source string. I'm not using snapshot functionality. The proxy isn't a problem as I've successfully used other methods.
Hope someone can help...
Many regards,
Steve
From Creating a Shared Access Signature:
The following table details which operations are allowed on a resource for a given set of permissions.
...
Create or update the content, block list, properties, and metadata of the specified blob. Note that copying a blob is not supported.

Object reference not set to an instance of an object Error

I'm trying to save text fields in MySql using NHibernate in Asp.Net frame work. But while executing this I got the error"Object reference not set to an instance of an object". If anyone knew help me...
Here is the code.
if (!Page.IsPostBack)
{
IList siteList;
ISessionFactory factory = new NHibernate.Cfg.Configuration().Configure().BuildSessionFactory();
using (ISession session = factory.OpenSession())
{
ICriteria sc = session.CreateCriteria(typeof(Employee ));
siteList = sc.List();
session.Close();
}
factory.Close();
}
**Can you post a stack trace? And a description of how you are configuring **
If i had to take a guess the crash is in (because of missing/incorrect configuration information):
new NHibernate.Cfg.Configuration().Configure().Build().SessionFactory()

Resources